I just found this example in my book.
After running the program in will only ready the first line of my file.

Ex. inside my file "PhoneBook.txt"

Mine 12345678
Hers 1234567
Them 123456

It will only read "Mine 12345678" ignoring "Hers 1234567 and Them 123456" How will i'll make this program ready all the text inside my file?

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

main()
{
    FILE *file;
    char temp[190];
    
    if ((file = fopen("PhoneBook.txt", "r")) != NULL)
    {
              if(fgets(temp, sizeof(temp), file) != NULL)
              {
                             printf("%s", temp);
                             fclose(file);
              }
              else printf("ERRor");
    }
    else printf("error");
    
    getchar();
    getchar();
    return 0;
}

Recommended Answers

All 3 Replies

> if(fgets(temp, sizeof(temp), file) != NULL)
To read the whole file, do
while(fgets(temp, sizeof(temp), file) != NULL)

Oh, and remove the fclose() from inside the loop.

Or better yet, leave it there and learn something ;)

can try,
while(fgets(temp, sizeof(temp), file) != NULL)

or u can use double array

char temp[100][190];
int count=0;
while(fgets(char temp[count],190,file)!= NULL)
{
      printf( "%s\n" ,temp[count]);
count++;
}
fclose (file);

p/s:im beginner too

thank you Salem...Sorry for the late reply...I was got busy lately...
Anyway i'll mark this as Solved.

Thank you so much...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.