EOF not working in a loop

This is a discussion on EOF not working in a loop within the C++ Programming forums, part of the General Programming Boards category; I used EOF in a C++ program to terminate a loop.. but it is not working.... could someone help me ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    EOF not working in a loop

    I used EOF in a C++ program to terminate a loop.. but it is not working.... could someone help me and tell me what im doing wrong.. the code for my program is..:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <io.h>
    int main(int argc, char * argv[])
    {
      if(argc!=2)
      {
        cout<<"Correct input is: filename";
        return 0;
      }
      if(access(argv[1], 00))
      {
        cout<<"File does not exist";
        return 0;
      }
      ifstream the_file;
      the_file.open(argv[1]);
      int x = 0;
      char placeInFile;
      the_file.get(placeInFile);
      while(placeInFile!=EOF)
      {
        if (placeInFile=='\n')
          x++;
        the_file.get(placeInFile);
      }
      cout<<"That file has "<<x<<" lines."<<endl;
      return 0;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    EOF is an int.
    use....

    Code:
    int placeinfile;
    placeinfile = file.get();
    then all will be well
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Ahhh I see, thank you

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with the eof loop
    By computernoob in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2009, 05:41 AM
  2. is there a way to replace EOF in this loop..
    By transgalactic2 in forum C Programming
    Replies: 32
    Last Post: 01-05-2009, 12:39 PM
  3. switch loop not working
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 10-29-2008, 01:54 PM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21