Thread: Moving EOF's position in a file

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Moving EOF's position in a file

    Hello, I'm trying to read input from a file to the screen using the fstream library. But what i want to do is to shift the position of EOF to make the input stop at supposedly half the file and not at the end.

    Any help is appreciated.
    Last edited by tonit; 01-02-2011 at 01:03 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As you read, check for that value. When you detect it, stop reading.

    Quote Originally Posted by tonit
    But what i want to do is to shift the position of EOF to make the input stop at supposedly half the file and not at the end.
    Right, so your requirements changed

    Read the entire file and discard the second half, or find out the size of the file and then read just the first half.
    Last edited by laserlight; 01-02-2011 at 01:20 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thank you for your quick reply. But i'm curious if i can play around with EOF. So i can make EOF(Supposedly) "End of half-file" instead of "End of File".

    Hope i expressed my point clearly here.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tonit
    But i'm curious if i can play around with EOF. So i can make EOF(Supposedly) "End of half-file" instead of "End of File".
    No. EOF is just a macro for a negative value used to indicate end of file has been reached.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is to say, at least on pretty much every system still running, there is no actual "end-of-file" character in a file.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't decrease a file's length on some OSes. You can under Windows, but not under Linux afaik. In Linux I'm told that you read in the file and write half of it out again and then delete the original file. So, what OS?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... so, are you trying to truncate the file to half its length, or are you trying to only read half of the file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Quote Originally Posted by iMalc View Post
    You can't decrease a file's length on some OSes. You can under Windows, but not under Linux afaik. In Linux I'm told that you read in the file and write half of it out again and then delete the original file. So, what OS?
    I'm working under the windows platform.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    SetEndOfFile Function (Windows)
    Bookmark the top level if you're into Win32 programming.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    In Linux I'm told that you read in the file and write half of it out again and then delete the original file.
    That would be insane. You just call truncate().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM