0

I want to write a program in C which just reads a file, stores it into an array and then prints the array. Everything works fine but when the text file has more than one line, I always just get the last line printed out.

This is my Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[]) {
   FILE * stream;
   char dateiname[255];
   stream = fopen("heute.txt", "r");

   if(stream == NULL){
      printf("Error");
      }else {
          while(!feof(stream)){
             fgets(dateiname, 255, stream);
      }
    fclose(stream);
    }

printf("%s\n", dateiname);

}

Thanks for help!

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
chrizzla
  • 53
  • 8
  • 2
    You only see the last item because you read all of the data into the same variable. Each line replaces the previous line and when you get to the end you print what's in that buffer. You can move the `printf` to print each line as it is read. You should also read this: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Retired Ninja Nov 12 '20 at 12:04
  • I suggest you do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) of your code. Or use an actual debugger to step through the code. – Some programmer dude Nov 12 '20 at 12:06
  • 1
    `while(!feof)` is always a bug. Search this site why it is so. – Jens Nov 12 '20 at 12:13
  • What is the purpose of your assignment or exercise? Just to copy the contents of the file to the console? To store the data and perform operations on it? Something else? Please [edit] your question to include the full text of your assignment or exercise. – Some programmer dude Nov 12 '20 at 13:12

2 Answers2

1
  1. Everything works fine but when the text file has more than one line, I always just get the last line printed out

Reason: For every iteration, the data gets replaced with the next line data, and at the end dateiname will read only the last line.

  1. while(!feof(stream))

Usage of feof() is not recommended. Please see this link for more information :https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284351&answer=1046476070

  1. Please see the following code:

     #include <stdio.h>
     #include <stdlib.h>
    
     int main()
     {
       FILE *stream;
       char dateiname[1024];
       int i = 0;
       stream = fopen("heute.txt", "r");
    
        if (stream == NULL)
        {
           printf("Error");
        }
        else
        {
            while (fgets(dateiname, sizeof(dateiname), stream) != NULL)
            {
               printf("Line %4d: %s", i, dateiname);
               i++;
            } 
        }
    
     return 0;
    }
    
Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
0

If you want to just read and print the contents of the file you no need to worry about the size of the file and how many number of lines you have in file.

you can just run fgets() in the while and print each line until we reach NULL

But if you want to store them, we need to calculate the size of the file. So we need to use functions like stat or fstat to get the size of the file and allocate memory dynamically then just read that many bytes.

csavvy
  • 761
  • 4
  • 13
  • The functions `stat` and `fstat` are not part of the ISO C standard. They are platform-specific extensions. Note that the question does not specify any particular platform. As far as I know, there is no reliable way to determine the file size in ISO C. For example, using `fseek` to go to the end of the file and then using `ftell` will not work reliably on all platforms, because the end of line markers can have a different length on different platforms. For example, Windows uses `\r\n` which is translated to `\n`. – Andreas Wenzel Nov 12 '20 at 16:59