Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i execute the below program on giving a string as a 'welcome' , then the 'welcomee' is displayed instead of 'welcome' . in the output why the letter 'e' of welcome is repeated?

C++
int main()
{
    char ch[10],c;
    fstream f;
    f.open("text",ios::out);
    cout<<"Enter a string :";
    cin>>ch;
    f<<ch;
    f.close();
    
    f.open("text",ios::in);
    while(f)
    {
            f.get(c);
            cout<<c;
    }
    f.close();
    getch();
    return 0;
}
Posted
Updated 15-Mar-12 6:55am
v2
Comments
Sergey Alexandrovich Kryukov 15-Mar-12 14:18pm    
Why not using std::string instead those brain-damaging null-terminated strings, once you already use std?
--SA

What happens if at cin>>ch the user types more than 9 characters?
What happens if the user type some spaces ?

If you want to use fixed buffers and null-terminates C strings, you have always to take care of the space and of the null-termination.
Also, to "pause at the and", yopu must ensure that no unread input is present.
The way to do that is discard every residual input after the read of the string.

C++
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <limits>

int main()
{
    using namespace std;

    const unsigned BUFFSZ=9;
    char ch[BUFFSZ+1];
    memset(ch,0,sizeof(char)*(BUFFSZ+1));
    char c=0;

    fstream f;
    f.open("text",f.out);
    cout<<"Enter a string :";
    // read up to BUFSZ chars
    cin >> std::setw(BUFFSZ) >> ch;
    //discard exceeded input up to the end of line
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

    f<<ch;
    f.close();


    f.open("text",f.in);
    while(f.get(c))
    {
        cout<<c;
    }
    f.close();

    cin.get(); //pause

    return 0;
}


The above program is safe, but reads the string up to the first space and up to 9 character (note the ch is 9+1 since there must be the space for the null character string terminator.

Note that f.get(c) is used in the loop condition: in your original loop, when you read the last character with f.get(c), the file is still "good", so you make another turn, f.get(c) fails, (making the file bad)and c is left the the previous character (hence the repetition in printing)

If you want to be more safe, you should use std::string that uses dynamic memory to accommodate whatever length:

C++
#include <iostream>
#include <fstream>
#include <string>
#include <limits>

int main()
{
    using namespace std;
    string ch; //no more an array
    char c=0;

    fstream f;
    f.open("text",f.out);
    cout<<"Enter a string :";
    getline(cin, ch);

    f<<ch;
    f.close();

    f.open("text",f.in);
    while(f.get(c))
    {
        cout<<c;
    }
    f.close();

    cin.get(); //pause

    return 0;
}

There is no more need to take care about the length, the entered text can contain spaces and the line is read to the end (so no cleanup is required).
 
Share this answer
 
If you run it under the debugger, you'll see why.
 
Share this answer
 
That's because the last time through the loop, f.get(c) doesn't return any new data.

To see what I mean, simply set c = 0x0 right before you retrieve each char. When done, the last letter is not repeated.
 
Share this answer
 
Using tip in solution #3

C#
int main()
{
    char ch[10],c;
    fstream f;
    f.open("text",ios::out);
    cout<<"Enter a string :";
    cin>>ch;
    f<<ch;
    f.close();

    f.open("text",ios::in);

    while(f.good())
    {
            // When last character has been read the loop is still valid and gets to here.
            f.get(c);

            // Now we may have read past last character so check it before making boo-boo. 
            if(f.good())
             cout<<c;
    }
    f.close();
    getch();
    return 0;
}
 
Share this answer
 
v3
Shouldn't that while statement read,
C++
while(f.good())
?
 
Share this answer
 
Comments
enhzflep 15-Mar-12 14:46pm    
Nope.
Doesn't help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900