Thread: read a character from a *.txt

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    Unhappy read a character from a *.txt

    Hi

    here is my code:
    Code:
    int main()										
    {											
       ifstream OpenFile("test.txt", ios::in);
        
        char ch;
    
         
         while(!OpenFile.eof())
    
          {
    
                OpenFile.get(ch);
    
                cout << ch;
    			
          }
    
          OpenFile.close();
    	
           return 0;									
    												
    
    }
    if there is "123" in the test.txt file

    but it prints out 1233, there is extra "3", I don't know why, what should I do to prevent it? Thanks
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The problem is your loop. eof doesn't get set until you've read one past the end of the file. A few ways to fix your problem: One is to check the return value of get() in your loop condition, another is to make the loop an infinite loop and do an if(eof()) break; after the get(), and another is to call get() first, and reverse the order of get() and cout.
    Code:
    while(OpenFile.get(ch))
    {
          cout << ch;		
    }
    
    for(;;)
    {
         OpenFile.get(ch);
         if(OpenFile.eof())
              break;
    
         cout << ch;
    }
    
    OpenFile.get(ch);
    while(!OpenFile.eof())
    {
         cout << ch;
         OpenFile.get(ch);
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while(!OpenFile.eof())
    This is wrong, and you've been around long enough to know it's wrong and why.
    My best code is written with the delete key.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I usually use
    >while(OpenFile.good())
    Is that acceptable?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >>while(OpenFile.good())
    >Is that acceptable?
    Here's a research exercise for you. Are the semantics for good() any different than eof() when used as a loop condition?
    My best code is written with the delete key.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Ewww... exercise... that's evil.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>This is wrong, and you've been around long enough to know it's wrong and why.
    Doesn't it work in the example I gave?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Doesn't it work in the example I gave?
    Yes, it does. I didn't notice that you played an order trick in the body.
    My best code is written with the delete key.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    how about

    while(OpenFile)

    or

    while(OpenFile.get(ch))
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I didn't notice that you played an order trick in the body.
    I wouldn't have thought of it, if I didn't see a friend do it the day before (although in different circumstances). It was kind of a neat trick he used to help parse data

    >>while(OpenFile.get(ch))
    If you'd read my post, you might have seen that it was the first example I gave
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. Replies: 1
    Last Post: 07-24-2002, 06:33 AM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM