Igbe Chukwudi
Igbe Chukwudi

Reputation: 83

how to read multiple lines of a file in c

Am trying to read a file which contains the coordinate values for my code. each time i use scanf it reads only the first line...(60,70,200,200). my question is how do i make my code read all the contents of my file and print it out on the screen.here is my code and file.

 FILE.txt:
   S (60,70)(200,200)
   S (30,40)(100,200)
   S (10,20)(80,10)
   S (60,400)(700,200)
   S (160,70)(240,20)

 MY CODE:
 #include <stdio.h>
 int a;
 int b;
 int c;
 int d;
 int data[4][5];

int main ( int argc, char *argv[] )
 {

if ( argc != 2 ) /* argc should be 2 for correct execution */
{
    /* We print argv[0] assuming it is the program name */
    printf( "usage: %s filename", argv[0] );
}
else 
{
    // We assume argv[1] is a filename to open
    FILE *file = fopen( argv[1], "r" );

    /* fopen returns 0, the NULL pointer, on failure */
    if ( file == 0 )
    {
        printf( "Could not open file\n" );
    }
    else 
    {
       int i,j;
      for (j=0; j < 5; j++) 
       {
         for (i=0; i < 4; i++) {  
         fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);
           data[i][j] = (a, b, c, d);              
           printf("%d,%d,%d,%d\n",a, b, c, d);
          }
      }             

        fclose( file );
    }
  }
}

Upvotes: 2

Views: 10040

Answers (6)

MOHAMED
MOHAMED

Reputation: 43518

I used fgets to read file line per line and I used sscanf instead of fscanf

#include <stdio.h>
#include <string.h>

int data[4][5];
char line[128];

int main ( int argc, char *argv[] )
{

    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else
        {
           int i=0;
           line[0] = 0;
           while(fgets(line,sizeof(line),file))
            {
               sscanf(line, "S (%d,%d)(%d,%d)", &data[i][0], &data[i][1], &data[i][2], &data[i][3]);
               printf("%d,%d,%d,%d\n", data[i][0], data[i][1], data[i][2], data[i][3]);
               i++;
            }
            fclose( file );
        }
      }
}

Upvotes: 0

P.P
P.P

Reputation: 121387

If you are going to fscanf(), you have to careful as you have to make sure the file is in correct format with exact number of spaces like how read in fscanf().

I would recommend using fgets() then reading numbers from the string using sscanf().

data[i][j] = (a, b, c, d);

This line doesn't do what you think it does. You really don't need another loop as you read all numbers in a line at once.

   for (j=0; j < 5; j++) 
       {
         fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);
           data[j][0] = a;
           data[j][1] = b;
           data[j][2] = c;
           data[j][3] = d;

           printf("%d,%d,%d,%d\n",a, b, c, d);
          }
      }             

And change data[4][5] to data[5][4];

Upvotes: 0

Hua
Hua

Reputation: 48

  1. declar data[4][5] to data[5][4]
  2. modify the loop for (i=0; i < 5; i++)
    { fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);

         data[i][0] =a;
         data[i][1] =b;
         data[i][2] =c;
         data[i][3] =d;
         printf("%d,%d,%d,%d\n",a, b, c, d);
      }
    

Upvotes: 0

user707650
user707650

Reputation:

Try reading the newline as well:

 fscanf(file, "S (%d,%d)(%d,%d)\n", &a, &b, &c, &d);

But why the double loop: you're already reading 4 values at the same time. Instead, make it one infinite loop and check whether you've read 4 values; break when you did not read 4 values.

Lastly:

   data[i][j] = (a, b, c, d);

does not make any sense (as any decent compiler warning will tell you). This, instead, may be where you want your second loop: around the assignement, not the scanf statement.

Upvotes: 0

spin_eight
spin_eight

Reputation: 4025

Use while loop:

const int NumberOfValuesIamGoingToReadAtOnce = 4;

while (fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d) == NumberOfValuesIamGoingToReadAtOnce)
{
    // do some actions with obtained values
}

as fscanf returns number of read values by return value you can judge if EOF achieved or file wrongly written

Upvotes: 0

unwind
unwind

Reputation: 399833

You must check the return value of I/O calls such as fscanf(). If it's failing, it will return 0 without changing your variables.

Also, this:

data[i][j] = (a, b, c, d); 

Doesn't make a lot of sense in C. Remember that C doesn't have tuples like Python. The above is equivalent to:

data[i][j] = d;

Upvotes: 1

Related Questions