Reading a file line by line in C

I am trying to write some code that will open a file, read its contents line by line, and store each of these lines in an array.

First, I open the file and count the number of lines, each line has a fixed length, so I just do this:

char buf2[LINE_LENGTH]; int in2 = open("toSend2", O_RDONLY); int number_of_lines = 0; for (;;) { char* p2 = buf2; int count = read (in2, p2, LINE_LENGTH); if (count < 0) { printf("ERROR"); break; } if (count == 0) break; number_of_lines++; printf("count: %d \n",count); printf("File 2 line : %s", p2); printf("\n"); } close (in2); 

So far this has worked well, number_of_lines is really the number of lines in the "toSend2" file, and each of my printfs is the lines contained in this file.

Now with the number of lines, I create an array of lines, and then I basically look at the whole file again, but this time I would like to save each of the lines in the array (perhaps the best way to find from the number of lines in the file, but all I tried failed!)

  char * array[number_of_lines]; int b=0; int in3=0; in3 = open("toSend2", O_RDONLY); for (;;) { char* p3 = buf3; int count = read (in2, p3, LINE_LENGTH); if (count < 0) { printf("ERRORRRRRR"); break; } if (count == 0) break; array[b] = p3; b++; printf("count: %d \n",count); printf("FILE 2 LINEEEEEE : %s", p3); printf("\n"); } close(in3); 

This, of course, does not work: each of my printfs is a straight line plus the last line of the file, for example, the first printf will look like this:

FILE 2 LINEEEEEEE: "First line of file" "Last line of file"

And after this for loop, when I trace the contents of my array, each element in it is just the last line of the file. I think this is because I just put the same pointer (pointing to another line at that moment) in the array every time, but in the end it will point to the last line, so everything will be the last line.

How can I solve my problem?

ps: I just started C, so please don’t think that I even know the basic things about it :(

+2
source share
4 answers
  • To input I / O, use stdio, i.e. fopen() , fgets() and fclose() . You are using the much lower Posix I / O for no good reason.
  • You need to dynamically select each new row to save it in an array. You can use strdup() for this.
  • Remember that everything can go wrong; files may not open, lines may not be read, and memory may not be allocated. Check it out and act accordingly.
+6
source

You did not create an array of strings, you created a pointer to an array of strings. you need malloc your array in part2 to count strings * char number per string. then move your reading lines to each subsequent position of the array.

[edit]
one more thing .....
your lines are length X. The lines 'C' are not length X, they are length X + 1 :)
[/ Edit]

+1
source

why not use fopen then just use fgets to get each line

0
source

You can use stat to get the file size. Then number_of_lines = stat.st_size/LINE_LENGTH

If you do not need nul-terminated character strings, you can read the entire file into a single buffer. Set up an array of pointers if you really want them, or just use &buf[n * LINE_LENGTH] to get the beginning of line n.

To print a line with zero completion of a known length, you can use:

 printf("line %d = '%.*s'\n", n, LINE_LENGTH, &buf[n * LINE_LENGTH]); 

Let me know in the comments if you want to see the actual code.

0
source

All Articles