File i/o problems

This is a discussion on File i/o problems within the C++ Programming forums, part of the General Programming Boards category; guys, can you tell me whats wrong with this code. I included everything, no compiler errors but I need to ...

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    File i/o problems

    guys, can you tell me whats wrong with this code. I included everything, no compiler errors but I need to read the number, then the phrases underneath it. look at my code and the contents of my file.

    The stuff in my code
    Code:
    ifstream file_op ("Hello.txt");
        while(!file_op.eof())
        {
                             
                             file_op>>line_num;
                                                      
                             for(temp = 0; temp <= line_num; temp++)
                             {
                                      file_op.getline(question[temp].str, 200);
                                      cout<<question[temp].str<<endl;
                             }
                             
        }
        
        
        
        file_op.close();
    stuff in file "Hello.txt"
    Code:
    0
    Hello world reporting from file
    0
    Roger!
    Last edited by ay_okay; 06-16-2005 at 02:39 PM.

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    What is question[temp]? Why don't you just print the str?
    And I think that the for condition is temp <= line_num, since you initialize temp to zero.

  3. #3
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Quote Originally Posted by Mortissus
    And I think that the for condition is temp <= line_num, since you initialize temp to zero.
    I don't get what you're trying to say. I edited my post, doesn't work. Any oher suggestions?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You didn't say how it isn't working.

    My guess is that you need to ignore the newline after you read in the number. Simple solution - Add a file_op.ignore(); after reading in the number with operator>>.

    BTW, you are controlling your while loop poorly. If you move the file_op>>line_num code into the while (i.e. while (file_op>>line_num)), it will be more accurate at finding the end of your file.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Learning how to use your debugger or using cout to output variables at strategic places in your code combined with a cin statement to pause your program while you ponder whether the code did what you thought it would when you thought it would as a debugging tool will be invaluable to you as you continue in your programming.
    You're only born perfect.

  6. #6
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks Daved it worked with the file_op.ignore() and the while loop also improved my program. By the way, elad, what you say is true. I've done that so many times, but I didn't include it in the post. I could have figured this one out if I knew the ignore() function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. 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
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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