Problems displaying content of a text file

This is a discussion on Problems displaying content of a text file within the C++ Programming forums, part of the General Programming Boards category; This function displays to the screen the contents (words separated by spaces) of a text file. It gets a character ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Problems displaying content of a text file

    This function displays to the screen the contents (words separated by spaces) of a text file. It gets a character and checks if it is a blank space. If it is, a newline is displayed. If not, the character is displayed. This continues until the eof is encountered.

    The problem I have is that the last letter of the last word in the file is always doubled. For example, if the file contains the word "HELLO", then "HELLOO" is what is displayed.

    I've checked the file itself and it does contain the correct spelling of the word, only the getting or displaying seems to be the problem.

    Also, only the last word of a file is affected.
    For example:
    If the file contains,
    HELLO
    GOODBYE
    STOP

    Then what is displayed is:
    HELLO
    GOODBYE
    STOPP

    Maybe I need a cin.ignore() statement before the cout using eof as a delimiter but I'm not sure how.

    Thanks in advance!!!

    Here is the function:
    // Prints every word from already open file stream
    void PrintFile(ifstream& cinfile)
    {
    // To hold current character
    char charInFile;

    // Loop until end of file
    while (!cinfile.eof())
    {
    // Store selected char into charInFile
    cinfile.get(charInFile);

    // If blank, print new line
    if(charInFile == ' ')
    cout << endl;
    // Else, print the character
    else
    cout << charInFile;

    } // End while

    } // End PrintFile()

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ok....
    from whtat i see (b/c all we see is your function) you are checking a character after a character.... i think your problem is exactly in your last iteration.....

    last three character of your file are (a letter, a terminating character[or new line], and end of file)

    after the last letter is displayed, you iterate again and find the terminating character, that terminating character seems to be not assigned to the current character, so the previous character is printed again....after that the loop encounters an eof so it terminates.....

    i don't have the whole program but try this....

    in your preconditions of the while loop write


    while(!cinfile.eof() || !cinfile.eol())


    simpley states.... while cinfile doesn't encounter eof() OR and end of a line, then do ............


    try this.....

    good luck

    Regards,
    matheo917

  3. #3
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49
    your code works correctly on my computer (linux) with no double character error. go figure??
    Ivan

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Ivan is right... i just had a minute and compiled the code for this particular function, and it works properly....
    the problem has to be somewhere else....

    tell us what compiler u are using and it would be better if we would see your whole program....

    Regards,
    matheo917

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Thumbs up

    I'm using VC++ 6.0 (Standard I think). I actually got it to work though. I made else an else if before the cout to check if its the end of file.

    cin.eol() would return true if it encountered a line break in the file, correct? I bet that would've worked perfectly for me, only my file dosen't/won't contain any line breaks (but you guys wouldn't have known since I only posted the one function).

    Funny that my function worked for you guys. I have been getting a few weird results that no one else in my class has been getting (they've all been using Introductory Edition).

    Thanks for the help, both of you!
    Last edited by melissa; 11-12-2001 at 04:50 PM.

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    u're welcome....

    oki doki....

    keep up the good work....

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. displaying contents of a text file
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-26-2002, 01:05 PM

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