eof()

This is a discussion on eof() within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to read a whole file in, but apparently using eof() doesn't seem to work. For example, ...

  1. #1
    Unregistered
    Guest

    eof()

    Hi, I am trying to read a whole file in, but apparently using eof() doesn't seem to work. For example, when I have something like this:

    ifstream input_file(argv[1], ios::in);
    while (!input_file.eof())
    cout << "hello?";

    This continuously prints out "hello?" which means that the end of the file is not being found. Any suggestions would be appreciated. The input file, by the way, has 15 characters in it.

    Thanks in advance.

  2. #2
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    This should work:
    Code:
    #include <fstream.h>			
    #include <iostream.h>				int main(void){
    	char str[600];				//string array of 600
    	ifstream file;
    	file.open("C:\\test.txt"); 
    	while (!file.eof())//the end of the file(eof)
    	{
    		file.getline(str, 90);				cout << str << endl;			
    	}
    
    	file.close();				//close the file
    	return(0);
    	}
    havn't tested it. Just came up with it. Hope this helps.

  3. #3
    Unregistered
    Guest
    I tried your example...it also seems to be running in an infinite loop... =(

    I am using MS Visual Studio and don't understand why it doesn't work...seems simple enough, doesn't it?

    Thanks for your help though.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ifstream input_file(argv[1], ios::in);
    >while (!input_file.eof())
    >cout << "hello?";
    This does nothing to move the file pointer toward EOF, so of course it is an infinite loop, the file pointer doesn't move. If you input data inside the loop then it should work a bit better.
    Code:
    while ( input_file.good() ) {
      input_file.get();
      cout<<"Hello?\n";
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    Yes you are right. I am sorry. I initially left out the details of the loop to make it simpler.

    This is the actual loop I tried:

    while (!input_file.eof())
    {
    input_file.getline(buffer,MAX_BUF-1);
    input.push_back(buffer);
    }

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 03:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM

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