basic file input using C

Posted by user1781966 on Stack Overflow See other posts from Stack Overflow or by user1781966
Published on 2012-11-17T16:40:48Z Indexed on 2012/11/17 17:00 UTC
Read the original article Hit count: 188

Filed under:
|

So im working on learning how to do file I/O, but the book I'm using is terrible at teaching how to receive input from a file. Below is is their example of how to receive input from a file, but it doesn't work. I have copied it word for word, and it should loop through a list of names until it reaches the end of the file( or so they say in the book), but it doesn't. In fact if I leave the while loop in there, it doesn't print anything.

#include <stdio.h>
#include <conio.h>

int main()
{

  char name[10]; 
  FILE*pRead;

  pRead=fopen("test.txt", "r");
  if (pRead==NULL)
    {
     printf("file cannot be opened");
    }else      


     printf("contents of test.txt");
     fscanf(pRead,"%s",name);
      while(!feof(pRead))
           {
            printf("%s\n",name);
            fscanf(pRead, "%s", name);
           }


             getch();
   }

Even online, every beginners tutorial I see does some variation of this, but I can't seem to get it to work even a little bit.

© Stack Overflow or respective owner

Related posts about c

    Related posts about file-io