Thread: reading untill the end of file?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    reading untill the end of file?

    how can i stop the reading cycle on eof:

    while(f!=EOF) tells me comparison betwen pointer and integer

    thank you!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm guessing you're trying to read one character at a time:
    Code:
    int ch;
    
    while((ch = fgetc(f)) != EOF)
      printf("I read the character: %c\n", ch);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Code:
    while("f!=EOF")
            {
                    fscanf(f,"%s",a);
                    printf("%s\n",a);
            }
    i`m trying to do smthing like this to read in a string a line

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try while(fgets(a, buffer_size, f)) instead.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    but i don't know the size of the line... its variable

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's not the size of the line you're supplying, it's the size of the buffer in which you want to store the line...
    If you understand what you're doing, you're not learning anything.

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You could use
    Code:
    while (!feof(f)) { ... }
    assuming that f is a FILE *. Also, in your fscanf() calls, you ought to be passing a pointer to the destination.
    [edit]
    I do prefer fgets() and sscanf() to the use of fscanf(); but that's been covered many places many times.
    [/edit]
    [edit2]
    As is pointed out below, feof() is not a good way to control the loop, and for good reasons. My contribution is simply in answer to the original query where the attemtp to use "if (f == EOF)" was being discussed.
    [/edit2]
    Last edited by filker0; 01-20-2006 at 01:57 PM. Reason: Add a comment...
    Insert obnoxious but pithy remark here

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by spank
    how can i stop the reading cycle on eof:
    The more correct idiom is "while successfully obtaining expected input" (and itsme86 has already posted such an example) rather than "while not having encountered one of several possible failures the last time".

    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's how to use fgets():
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        char s[BUFSIZ];
        FILE *fp;
    
        if(argc <= 1) {
            fprintf(stderr, "\nusage: printfile file_to_print\n");
            return 1;
        }
    
        if((fp=fopen(argv[1], "rt")) == NULL) {
            fprintf(stderr, "\nCan't open file \"%s\"\n", argv[1]);
            return 1;
        }
    
        while(fgets(s, sizeof(s), fp)) {
            printf("%s", s);
        }
    
        fclose(fp);
    
        return 0;
    }
    BUFSIZ is defined in <stdio.h>, and it's usually about 512. You can use any number that you like.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM