reading text files

This is a discussion on reading text files within the C++ Programming forums, part of the General Programming Boards category; I have an add filename function that saves the filenames to a text file like so: Code: outfile.open("database.txt", ios::app); outfile ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    reading text files

    I have an add filename function that saves the filenames to a text file like so:

    Code:
    outfile.open("database.txt", ios::app);
    outfile << filename << endl;
    outfile.close();
    and retrieves the filenames from another function to manipulate them with

    Code:
    infile.open("database.txt");		
    
    while (!infile.eof())
    {
    	infile.getline(file,80);
    	vfn.push_back(file);
    
    }
    infile.close();

    This crashes at the last infile.open("database.txt") call because of where the cursor is positioned in the saved text file and I guess it tries to read the last line which doesn't have any text in it

    Code:
    TEXT FILE
    
    filename 
    filename
    filename  
    cursor position
    If I manually save the file with the cursor position with the last end of line command removed, it works perfectly

    Code:
    TEXT FILE THIS WORKS
    
    filename 
    filename
    filenamecursor position

    The problem is, once it's saved like that, when I return to my add file function I obviously have a little problem

    Code:
    TEXT FILE
    
    filename 
    filename
    filenameFILENAME //should not be here
    cursor position

    Basically, I just don't know how I can save my file in order to be able to retrieve it correctly.

    Do I need to put " endl" at the end of each filename I save on the output?

    If I don't, it is not only on one line so how do I retrieve only one filename at a time?

    Any suggestions? Thanks in advance!!!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    This has come up a lot lately:
    Code:
    while (!infile.eof())
    doesn't work how you expect it to, because the eof() condition is true after an attempt to read fails. Change your code to check for eof() after the read but before the push_back.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    If you mean

    Code:
    do
    {	
    	infile.getline(file,80);
    	vfn.push_back(file);
    
    }  while (!infile.eof());
    It has the same problem, the last loop still reads in the last line with no strings.

    and if you mean this by before the push_back

    Code:
    do
    {	
    	infile.getline(file,80);
    
    }  while (!infile.eof());
    
          vfn.push_back(file);
    then the push_back isn't in the loop, so it doesn't push back every filename...


    If that's not what you mean, then I don't know what you mean
    I am very confused...
    Last edited by stimpyzu; 04-15-2004 at 09:27 PM.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Well I found my solution, I just pop_back the last line before I start manipulating my data...

    Code:
    	while (!infile.eof())
    	{
    
    		infile.getline(file,30);
    		infile.ignore(0,'\n\n');
    		vfn.push_back(file);
    
    	}
    
    	vfn.pop_back();
    	infile.close();
    That way, I don't get that pesky last line and when I append, everything works fine!!!

    Just thought I'd post it if anybody has had the same problem...
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Actually u could edit the text file does that it does not have a newline at the last row, save all the problem.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    >Actually u could edit the text file does that it does not have a newline at the last row
    Technically, such a file is no longer a text file
    The easy answer is to not use any kind of while ( !eof ) type construct
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    SALEM I read the FAQ, it says to use fgets.... but isn't that in C? Is there a command in C++ I could use?

    Thanks for the link it was quite informative!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but isn't that in C?
    fgets is valid C++, but it isn't much of a stretch to imagine getline being used instead:
    Code:
    while (infile.getline(file,30))
    {
      ...
    }
    My best code is written with the delete key.

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by stimpyzu
    If you mean

    Code:
    do
    {	
    	infile.getline(file,80);
    	vfn.push_back(file);
    
    }  while (!infile.eof());
    It has the same problem, the last loop still reads in the last line with no strings.

    and if you mean this by before the push_back

    Code:
    do
    {	
    	infile.getline(file,80);
    
    }  while (!infile.eof());
    
          vfn.push_back(file);
    then the push_back isn't in the loop, so it doesn't push back every filename...


    If that's not what you mean, then I don't know what you mean
    I am very confused...
    Even though there are better ways of doing it (like putting getline into the loop condition), just for your information this is one way to check eof() after the read but before the push_back:
    Code:
    infile.getline(file,80);
    while (!infile.eof())
    {
        vfn.push_back(file);
        infile.getline(file,80);
    }
    and here's another:
    Code:
    while (true)
    {
        infile.getline(file,80);
        if (infile.eof())
            break;
        vfn.push_back(file);
    }

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi prelude, here is a question on your code .... getline() does not return boolean things . it returns streams (reference to istream ). so how your while loop is going to be invalid ??

    i uderstand your code is similar to jlou.

    jlou's code is breaking the loop while eof is achieved .

    Code:
    while (true)
    {
        infile.getline(file,80);
        if (infile.eof())
            break;
        vfn.push_back(file);
    }

    could you plz tell, how your while loop is going to be invalidated ? getline dont return bool.
    blue_gene

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >getline() does not return boolean things
    All I/O streams define an implicit conversion to void* that returns null on failure so that the stream can be used as a condition.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 02:17 PM
  2. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 04:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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