字符串,文件,将字符串保存到文件中。 SPACE不会被保存。 [英] string, file, saving a string into a file. SPACE's won't be saved.

查看:72
本文介绍了字符串,文件,将字符串保存到文件中。 SPACE不会被保存。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码如下:



Hi,
I have the code below:

string str1;
	ofstream fp("file1.txt");
	if(fp.is_open()){
		cout<<"\n Enter first string ::: ";
		cin>>str1;
		fp<<str1<<"\n";		
	}	





它有效,但它不会保存我输入的整个字符串。
例如
,我输入:



Hello world!



在它写的文件中:



你好



直到它保存到文件的句子中的第一个空格,之后,我不知道会发生什么,它只是不保存它们。



为什么它会像它一样工作?

是吗字符串问题或'fp<< '?



我该如何解决?



谢谢,



It works but it doesn't save tho whole string which i type as the input.
for example, i type:

Hello world!

in the file it's written:

Hello

until the first blank space in the sentence it save to a file, after that, i don't know what happens, it just don't save them.

Why does it work like it?
Is it a problem with the string or with the ' fp<< ' ?

How should i fix it?

Thanks,

推荐答案

因为空白是 cin 的字符串分隔符。您应该使用(及其警告......) getline [ ^ ]。
Because the blank is a string separator for cin. You should use (with its caveats...) getline[^].


这篇关于字符串,文件,将字符串保存到文件中。 SPACE不会被保存。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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