在输出中为什么要重复欢迎字母"e"? [英] in the output why the letter 'e' of welcome is repeated?

查看:94
本文介绍了在输出中为什么要重复欢迎字母"e"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下程序以将字符串作为"welcome"提供时,则显示"welcome"而不是"welcome".在输出中为什么要重复欢迎字母"e"?

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;
}

推荐答案

如果在cin>> ch中键入的字符超过9个,会发生什么情况?
如果用户键入一些空格会怎样?

如果要使用固定缓冲区和以null终止的C字符串,则必须始终注意空间和以null终止.
另外,要在and处暂停",yopu必须确保不存在未读输入.
这样做的方法是在读取字符串之后丢弃所有残余输入.

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;
}



上面的程序是安全的,但是可以读取字符串,直到第一个空格为止,最多可以读取9个字符(请注意ch为9 + 1,因为必须有用于空格字符串终止符的空格.

请注意,在循环条件中使用了f.get(c):在您的原始循环中,当您读取带有f.get(c)的最后一个字符时,文件仍然是" good ",因此您要转一圈f.get(c)失败,(使文件),并且c留在前一个字符(因此在打印中重复)

如果您想更加安全,则应使用std::string,该std::string使用动态内存来容纳任意长度:



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).


如果在以下位置运行它调试器,您会明白为什么.
If you run it under the debugger, you''ll see why.


那是因为最后一次循环,f.get(c)不会返回任何新数据.

要了解我的意思,只需在获取每个字符之前设置c = 0x0.完成后,将不重复最后一个字母.
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.


这篇关于在输出中为什么要重复欢迎字母"e"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆