0

I want to read below lines in input.txt file in C program and assign it to variables.

 (1,3) (4,8)
 (3,6) (3,10)
 (2,6) (4,56)

...it has to read more than 100 lines and draw lines with 4 values in each line.

I was using fgets and sscanf, but I am not able to get the value and below was my code.

static const char filename[] = "src/input.txt";
FILE *file = fopen ( filename, "r" );

if ( file != NULL )
{
    char line [ 128 ]; 
    int i,j,k,l=-1;

    while ( fgets ( line, sizeof line, file ) != NULL )
    {
        fputs ( line, stdout ); /* write a line */            
        sscanf (line,"%d" "%d" "%d" "%d", i,j, k,l); 
    }
    fclose ( file );
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    just show what you tried !!! – ThisaruG Sep 20 '14 at 23:21
  • See this: http://stackoverflow.com/questions/9206091/going-through-a-text-file-line-by-line-in-c and this: http://stackoverflow.com/questions/4600797/read-int-values-from-a-text-file-in-c – ROMANIA_engineer Sep 20 '14 at 23:29
  • I want to read four integers in each line and draw a line and I can't paste my code because its giving so many errors and I have been trying for half an hour – user3091801 Sep 20 '14 at 23:44
  • 1
    As you go further in C programming, take a look at `getline` for line input as opposed to `fgets`. The primary advantage is that if you provide an unallocated pointer to `getline` it will allocate space as needed in addition to returning the number of characters successfully read. `fgets` is fine here, but put `getline` in your hip pocket for later. – David C. Rankin Sep 21 '14 at 00:46

1 Answers1

0

In scanf replace i, j, k,l with addresses &i, &j,&k,&l

radar
  • 13,270
  • 2
  • 25
  • 33