首页 新闻 赞助 找找看

求大虾帮忙指点

0
悬赏园豆:5 [已解决问题] 解决于 2013-04-15 16:29
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    string content;
    cout << "input your content:" << endl;
    cin >> name;
    ofstream outfile("notebook.txt");
    if (!outfile)
    {
        cout << "File cannot be opened." << endl;
        return 0;
    }
    cout << "what you input is:" << content << endl;
    outfile << content;
    outfile.close();
    string show;
    cout << "do you want to show the file?0/1" << endl;
    int i;
    cin >> i;
    if (i = 0)
        return 0;
    ifstream infile("name.txt");
                  infile >> show;
    cout << "the file is:" << endl;
    cout << show;
    infile.close();

}

本人在学习输入输出流时,想做一个记事本,但是发现如果输入内容中有空格号就会丢失后面的内容,请问大虾如何能把我要记录的内容原原本本地保留下来?是基于输入输出流的知识的哦,感谢

c++
kebin00的主页 kebin00 | 初学一级 | 园豆:196
提问于:2012-12-08 17:39
< >
分享
最佳答案
1

string strContent;

getline(cin, strContent);

一切问题解决!

收获园豆:2
风小手 | 菜鸟二级 |园豆:248 | 2013-04-15 15:38
其他回答(3)
1

用getline()函数,遇到空格或eof结束符才会停止接收输入。想接收多行输入用while循环,在调整下格式应该就可以了,自己搜一下函数用法吧。

收获园豆:1
comeby | 园豆:203 (菜鸟二级) | 2012-12-09 14:15
1

用流对象成员函数进行输入输出格式控制,可以用getline进行输入。

收获园豆:1
被逼蒙蔽了双眼 | 园豆:204 (菜鸟二级) | 2013-01-07 10:56
1

同意楼上,lz这个问题好像是输入被空格切割成了两个或两个以上的参数。

另外

if (i = 0) // 这里写错了。应该是==吧

   return 0;

收获园豆:1
jogger | 园豆:211 (菜鸟二级) | 2013-01-07 11:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册