944,030 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 187042
  • C RSS
2

Reading a File Line By Line

by on Jan 12th, 2005
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.
C Code Snippet (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. static const char filename[] = "file.txt";
  6. FILE *file = fopen ( filename, "r" );
  7. if ( file != NULL )
  8. {
  9. char line [ 128 ]; /* or other suitable maximum line size */
  10.  
  11. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  12. {
  13. fputs ( line, stdout ); /* write the line */
  14. }
  15. fclose ( file );
  16. }
  17. else
  18. {
  19. perror ( filename ); /* why didn't the file open? */
  20. }
  21. return 0;
  22. }
  23.  
  24. /* file.txt
  25. This text is the contents of the file named, "file.txt".
  26.  
  27. It is being used with some example code to demonstrate reading a file line by
  28. line. More interesting things could be done with the output, but I'm trying to
  29. keep this example very simple.
  30. */
  31.  
  32. /* my output
  33. This text is the contents of the file named, "file.txt".
  34.  
  35. It is being used with some example code to demonstrate reading a file line by
  36. line. More interesting things could be done with the output, but I'm trying to
  37. keep this example very simple.
  38. */
Comments on this Code Snippet
Feb 9th, 2006
0

Re: Reading a File Line By Line

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.
Newbie Poster
khuman_nb is offline Offline
3 posts
since May 2004
Mar 19th, 2006
0

Re: Reading a File Line By Line

Quote ...
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.
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 24th, 2006
0

Re: Reading a File Line By Line

hi this is great!

can you tell me how do i put each line to an array?
Newbie Poster
darylcharm is offline Offline
4 posts
since Aug 2006
Jan 8th, 2007
0

Re: Reading a File Line By Line

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[i][j] = '\0';

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

if ( file != NULL )
{

i=0;

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

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

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


return 0;
}
Tmo
Newbie Poster
Tmo is offline Offline
1 posts
since Jan 2007
Nov 12th, 2008
0

Re: Reading a File Line By Line

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
Newbie Poster
tirmizi is offline Offline
1 posts
since Nov 2008
Feb 13th, 2009
0

Re: Reading a File Line By Line

thanks for this code this is just what im looking for..
Newbie Poster
Dionysus is offline Offline
8 posts
since Mar 2007
Aug 24th, 2010
0

Re: Reading a File Line By Line

Im very much thankful to you for providing the coding for me
Newbie Poster
swaythi is offline Offline
2 posts
since Aug 2010
Aug 24th, 2010
0

Re: Reading a File Line By Line

[COLOR="green"]thank you for the code..!!![/COLOR]
Newbie Poster
swaythi is offline Offline
2 posts
since Aug 2010
Dec 20th, 2010
0

Re: Reading a File Line By Line

I tried using this code, but it gives a segmentation error when trying to read the last line of the file
Newbie Poster
bojomojo is offline Offline
24 posts
since Aug 2008
Message:
Previous Thread in C Forum Timeline: Power of 2
Next Thread in C Forum Timeline: Using Or and And help with this segment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC