pointers into largish text files

This is a discussion on pointers into largish text files within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to use the ifstream function seekg() to reposition a file pointer to the beginning of a ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    pointers into largish text files

    Hi,

    I am trying to use the ifstream function seekg() to reposition a file pointer to the beginning of a text file. My code does not work when I use a text file that is about 6MB (which is the size of the data files I am processing). However, my code works just fine when I use a smaller file. Does anybody have any advice?

    Thank you,

    donkeypunch

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    OS?
    Compiler?
    std-lib provider?
    sample code?

    gg

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Thanks for your reply.

    I'm using Borland C++ Builder 5.0 in Windows XP Pro.

    Here's a sample:

    char line[200];
    ifstream infile("filename");
    while(!infile.eof())
    {
    infile.getline(line, sizeof(line));
    ... //do stuff with line
    }
    long pos1 = infile.tellg();
    infile.seekg(0);
    long pos2 = infile.tellg();
    ...

    When I run this on a test file that is 860840 bytes long, pos1 contains 860840 and pos2 contains 0, just as I would expect. When I run this on a data file that is 6311260 bytes long, pos1 and pos2 are both set to -1.

    Thanks again for any help you might offer.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    >> pos1 and pos2 are both set to -1
    That means that the fail bit is set for your infile stream. In other words, infile.fail() == true.
    You'll need to track down the cause of the failure next. Check the status of the stream after each input operation.

    gg

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Okay, so by checking for when it failed I found that the file pointer is at 6311260, then it does getline, then it infile.fail() becomes true.

    Sorry, for not doing this earlier, but I went to compare my data file and test file, and noticed that when I open the data file in notepad, there are two empty lines at the bottom. However, when I opened the test file in Notepad, there were no empty lines at the bottom. When I deleted the two blank lines on the data file, it started behaving in the way I wanted (namely, infile.fail() would not become true). Conversely, if I append two blank lines to the shorter test file, it starts to behave badly (infile.fail() becomes true).

    I guess I have two questions now.
    First, why do those two blank lines at the end make a difference?
    Second, the data files come to me with the two blank lines at the bottom, so, is there some way I can avoid generating a failure when I am reading the file line by line (perhaps by modifying my the while test)?

    Thanks for all of your help.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let me guess, you wrote a pair of NULLs to your file?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    My data file has two extra NULLs (blank lines) at the end. I am going to be dealing with a lot of these data files and this is how they come to me. When I remove the NULLs, the problem I described in the previous posts goes away.

    But since these data files come from another program with the two NULLs on the end, I kinda need to deal with them as is.

    I'm pretty new to this stuff and I was wondering why the NULLs would make any difference. Why does the presence of NULLs at the end of the file make infile.getline() to cause infile.fail() to become true?

    Also, how can I get around this problem without removing the NULLs from the data file? I guess I need to modify what I test in my while condition, but I do not know the best way to do that.

    I guess this is just a rewording of my previous post but hopefully it makes my problem a little more clear.

    Thank you,

    donkeypunch

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Are there actually NULL characters at the end of the file or just two blank lines?

    Search this board or the FAQ section of this site to find out why using eof() as a conditional statement isn't a good idea.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Well, I don't really know.
    When I open the file in Notepad, the last two lines contain no characters or spaces.

    I ended up using infile.clear() after my while loop and before trying to reset the pointer to the beginning of the file. That seems to work just fine.

    Thanks to everybody for your help. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  5. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM

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