1,105,546 Community Members

Reading a File Line By Line

Member Avatar
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
 
2
 

While not terribly difficult, first timers may not know the functions to look up and use. Here is some basic code for reading a file one line at a time. The contents of the file are printed to the stdout.

#include <stdio.h>

int main ( void )
{
   static const char filename[] = "file.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
      char line [ 128 ]; /* or other suitable maximum line size */

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         fputs ( line, stdout ); /* write the line */
      }
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
   return 0;
}

/* file.txt
This text is the contents of the file named, "file.txt".

It is being used with some example code to demonstrate reading a file line by
line. More interesting things could be done with the output, but I'm trying to
keep this example very simple.
*/

/* my output
This text is the contents of the file named, "file.txt".

It is being used with some example code to demonstrate reading a file line by
line. More interesting things could be done with the output, but I'm trying to
keep this example very simple.
*/
Member Avatar
khuman_nb
Newbie Poster
3 posts since May 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I thinks instead of depending on the length of the line (which can be variable depeding on the type of editor and its settings).I think it'll be better to look for '\n' which is line separator.

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

I thinks instead of depending on the length of the line (which can be variable depeding on the type of editor and its settings).I think it'll be better to look for '\n' which is line separator.

Then you would be describing doing this "by character" rather than by line. Otherwise you are wonderfully overcomplicating this with dynamic memory allocation a la C++'s std::string.

Member Avatar
darylcharm
Newbie Poster
4 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi this is great!

can you tell me how do i put each line to an array?

Member Avatar
Tmo
Newbie Poster
1 post since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi guys
i just modified the one i got here: this will read line by line from a text file then stored every line to an array;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>


int main ( void )
{
static const char filename[] = "data.txt";
FILE *file = fopen ( filename, "r" );
int i, j;
char arra[128][128];
char line[128]; /* or other suitable maximum line size */



for(i=0; i<128; i++)
for(j=0; j<128; j++)
arra[j] = '\0';


for(i=0; i<128; i++)
line = '\0';


if ( file != NULL )
{


i=0;


while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{


strcpy(arra, line);
printf("array ----> %s ", &arra);
i++;


}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}



return 0;
}
Member Avatar
tirmizi
Newbie Poster
1 post since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i want to Read a line from file then compare it to a sentense(GPS Sentence) and if its match(valid) then display this line else display Error

plz advice
Regards

Member Avatar
Dionysus
Newbie Poster
8 posts since Mar 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks for this code this is just what im looking for..:D

Member Avatar
swaythi
Newbie Poster
2 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Im very much thankful to you for providing the coding for me

Member Avatar
swaythi
Newbie Poster
2 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you for the code..!!![/COLOR]

Member Avatar
bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried using this code, but it gives a segmentation error when trying to read the last line of the file

Member Avatar
kanishka.keshav
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

may be you should avoid placing a newline character twice at the end of the file.
coz after the new line character it will try to read the next line but the fileEnd comes up

Member Avatar
zoidac
Newbie Poster
1 post since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Good Afternoon, i would like to ask you a question.

I have got file which structured as
"point id" "x coordinates" "y coordinates" "z coordinates"

I would like to open file and search for point id then if point exist in the file
I would like to use x,y and z coordinates of the point.

I will call 4 point like that and calculate the cross-section point coordinates
then write it on file by giving point id by user.

and I also would like to chech if the point id giving by user is exist in the file too.

I dont need the calculation part, i will match variables to x1,y1,z1, ..... x4,y4,z4,
and calculate x5,y5,z5 but i couldnt manage other things.

I will be glad to have any help.

Member Avatar
'CoffeeLike
Newbie Poster
1 post since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank

You
Post:
Start New Discussion
Tags Related to this Article