Thread: [question] ifstream read file, thanks!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    Cool [close] ifstream read file, thanks!

    ---------------------------------------------
    My config.ini content:
    c:\program\edit plus\editplus.exe
    ---------------------------------------------

    Abnormal Output: c:\program\editplus\editplus.exe [the blank missed between edit AND plus?]
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void main() //the program starts here
    {
    	
    	ifstream OpenFile("config.ini");
    	char ch[80];
                    int i;
    	while(!OpenFile.eof())
    	{
    	OpenFile >> ch;
    	cout << ch;
    	}
    	cout << endl;
    	OpenFile.close();
    
    	cin >> i; 
    }

    Abnormal output: c:\program\edit plus\editplus.exee (double e)
    Why ?
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void main() //the program starts here
    {
    	
    	ifstream OpenFile("config.ini");
    	char ch;
    	int i;
    	while(!OpenFile.eof())
    	{
    	 OpenFile.get(ch);
    	 cout << ch;
    	}
    	cout << endl;
    	OpenFile.close();
    
    	cin >> i; 
    }
    Last edited by userpingz; 05-19-2009 at 05:34 AM. Reason: better style for reading,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because eof is not set until the last character has been read AND you have attempted to read the next one. Change your code to test the success of the read itself, e.g.
    Code:
    while(OpenFile >> ch)
    {
    ...
    }
    Then you won't process a character AFTER eof has been reached.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Great!
    i test as your said, more simple code.

    thanks again!

    -------------------------------------------------------------

    And i test more carefully, i find new issue.
    The BLANK SPACE between edit AND plus missed ? I comment at the 1st floor.

    I can't find a right way to read this folder path which including a BLANK SPACE.
    i tried the function, it is the same result.
    Code:
    while(OpenFile >> ch)
    {
    ...
    }
    There is someting wrong?
    How to read BLANK places with ifstream?
    Any more suggestion, thanks!
    Last edited by userpingz; 05-19-2009 at 04:22 AM. Reason: find new issue.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To read in text with spaces you would generally use getline instead of operator >>.

    I'd suggest using the version that works with the string class, and switching to use the string class instead of C style strings, but there is a version of getline that works either way.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Get it.
    Very appreciated for above of your kindly help!

    -------------------------
    Code:
    #include "stdafx.h"
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () 
    {
      string strInfo;
      int i;
    
      ifstream OpenFile ("config.ini");
      if (OpenFile.is_open())
      {
        while ( ! OpenFile.eof())
        {
          getline (OpenFile, strInfo);
          cout << strInfo << endl;
        }
        OpenFile.close();
    	cin >> i;
      }else 
        cout << "Unable to open file"; 
    
      return 0;
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while ( ! OpenFile.eof())
    {
        getline (OpenFile, strInfo);
        cout << strInfo << endl;
    }
    That's still incorrect for the same reason matsp mentioned previously. Replace the eof test in the while loop's conditional with the getline call itself.
    Code:
    while ( getline(OpenFile, strInfo) )
    {
        cout << strInfo << endl;
    }
    In general, there seem to be few instances where an eof test in the conditional part of a loop is done correctly.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    :-) . thank you!
    So far i really enjoyed from the case!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM
  4. cannot read long file name
    By calvinlkn in forum C Programming
    Replies: 5
    Last Post: 04-11-2002, 05:04 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

Tags for this Thread