Thread: how to check the end of file?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to check the end of file?

    how to check the end of a txt file??

    Code:
    if(file.eof()){.....}
    ???


    thank you for helping~~~

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    solved....but

    oh.......that's correct, but how to move back to the begin of file???

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    is it the only method?

    well error found cos i think i open the file using

    Code:
    ifstream file(.......)
    is there any other method?

  5. #5
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    seekg!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you but....

    thank you ... i have use the seekg function and i have also use tellg and it said that the position is zero.

    but when i use getline to get the character, the character is empty.

    what is the problem??
    Code:
    if(file7.eof())
    	{						cout << "eof" << endl;				cout << file7.tellg() << endl;
    	file7.seekg(0);
    	file7.getline(RFQ,14);
    	cout << "||" << RFQ << "||" <<endl;
    	file7.seekg(0);
    	cout << file7.tellg() << endl;
    	}
    	if (file7.eof())
    	{
    	cout << "again!!!" << endl;
    	}
    out put:

    eof
    0
    ||||
    0
    again!!

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    if(file7.eof())
    	{						cout << "eof" << endl;				cout << file7.tellg() << endl;
    	file7.seekg(0);
    
    Here you could try adding a clear():
    file7.clear();

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    instead of
    file7.seekg(0);

    you could try
    file.seekg(0, ios::beg)

    to explicitly indicate you want to go back to beginning of file.

    Are you sure there is anything in the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2009, 12:24 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM