怎么解决空格问题?help me!!!!!!!!!!!!!!

woxihuanbohe 2002-09-22 02:36:05
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
ofstream outFile("copya.txt",ios_base::app);
if(!outFile)
{
cerr<<"can not open the file copya.txt!"<<endl;
return -1;
}

string aa;
while(cin>>aa)
{
if(aa=="0")
break;
outFile<<aa;
}
return 1;
}

怎么解决空格问题?
我输入如下:
string is here!!
yes,good!!
为什么文件输出却是stringishere!!yes,good!!
...全文
59 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
woxihuanbohe 2002-09-22
  • 打赏
  • 举报
回复
wait.........
woxihuanbohe 2002-09-22
  • 打赏
  • 举报
回复
自己顶一下
等高人出现!!!
woxihuanbohe 2002-09-22
  • 打赏
  • 举报
回复
多谢两位参与我的问题
我查了一下msdn 可是看得不太明白
其实 alexxing(赤铸) 对“>>"原理的陈述已经很清楚(对于解决这个问题)
">>"和get()确实是有区别的
还有一个问题我还不太明白;就是
输入变量或者说对象类型的问题 “>>”可以输入int, char 等类型 而get()呢?
好象只能是处理字符。
比如 int a; cin>>a;
而不能用get(a);

还有就是; 如果下面代码把outFile.close()去掉的话,更新文件的内容(即新输入的内容)就不能作为文件输入,这是为什么?
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
//从键盘输入字符写入磁盘文件
ofstream outFile("copya.txt",ios_base::app);
if(!outFile)
{
cerr<<"can not open the file copya.txt!"<<endl;
return -1;
}
/* char ch;
while(cin>>ch)
{
if(ch=='0')
break;
outFile<<ch;
}*/
char chs;
while(cin.get(chs))
{
if(chs=='0')
break;
outFile.put(chs);
}

outFile.close();//

//显示打开的文件内容
ifstream inFile("copya.txt");
if(!inFile)
{
cerr<<"can not open the file copya.txt!"<<endl;
return -1;
}
while(!inFile.eof())
cout.put(inFile.get());
inFile.close();
return 1;
}
alexxing 2002-09-22
  • 打赏
  • 举报
回复
SORRY,我又仔细看了STL的源程序,发现">>"和"get"还是有区别的:

operator>> 调用

template <class _CharT, class _Traits>
void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT& __c)

实现它的功能,其中有一句:

sentry __sentry(*this); // 跳过空格!

而 get() 对应的一句:

sentry __sentry(*this, _No_Skip_WS()); // 不跳过空格!

以前我光看帮助文档里的说明,以为都一样,抱歉
alexxing 2002-09-22
  • 打赏
  • 举报
回复
用 get 和 << 没区别

istream_type& get(char_type& c);

template<class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, charT& c);

两者的功能其实一样:

提取一个字符,将其存入 c。如果没有字符,调用 basic_ios::setstate(failbit), 可能 throw ios_base::failure.

只是前者是成员函数,后者是非成员函数
woxihuanbohe 2002-09-22
  • 打赏
  • 举报
回复
to:alexxing(赤铸)
用你如下的代码
char c;
while(cin >> c)
{
if(c == '0')
break;
outFile << c;
}

如果输入是so good
文件输入是sogood 空格丢失了
但是用
char ch;
while(cin.get(ch))
{
if(ch=='0')
break;
outFile.put(ch);
}
却可以解决这个问题!
woxihuanbohe 2002-09-22
  • 打赏
  • 举报
回复
to: alexxing(赤铸)
一开始我是定义string aa
然后用get()函数 ,可是发生参数类型不匹配
所以想到用<<操作付;
请问用get()和put()是不是和操作付达到一样的效果, 有什么大的区别吗?;
比如我现在写:
char ch;
while(cin.get(ch))
{
if(ch=='0')
break;
outFile.put(ch);
}
alexxing 2002-09-22
  • 打赏
  • 举报
回复
因为 ">>" 的原理就是连续读入一串字符,直到下列条件之一:
1. If is.width()>0, 读取了 is.width()-1 个字符
2. 遇到 EOF
3. 遇到 SPACE

while(cin>>aa)
{
...
}

每遇到一个空格字符,cin>>aa 返回一次,执行一次 {} 里的部分,空格本身被当作分隔符,没有放进 aa

最简单的办法就是用单个的字符代替字符串

char c;
while(cin >> c)
{
if(c == '0')
break;
outFile << c;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧