Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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?
 
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 15-Mar-12 7:28am
Edited 15-Mar-12 7:55am
v2
Comments
SAKryukov at 15-Mar-12 14:18pm
   
Why not using std::string instead those brain-damaging null-terminated strings, once you already use std?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
 
#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:
 
#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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you run it under the debugger, you'll see why.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Using tip in solution #3
 
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;
}
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Shouldn't that while statement read,
 while(f.good())
?
  Permalink  
Comments
enhzflep at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100