如何阅读包括空格在内的整行? [英] How to read whole line including white space?
本文介绍了如何阅读包括空格在内的整行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个程序,要求用户输入一些信息,然后将其输入到文本文件中,但我无法弄清楚如何读取整行中的空格。当用户输入包含空格的行时,只有第一个单词出现在文本文件中。任何人都可以帮我解决这个问题吗?在此先感谢。
这是我的程序:
I am making a program that asks the user to enter some information and then enters it into a text file, but I can''t figure out how to read a whole line with spaces in it. When the user enters a line with spaces in it, only the first word appears in the text file. Can anyone help me figure this out? Thanks in advance.
Here is my program:
展开 | 选择 | 换行 | 行号
推荐答案
我正在制作一个程序,要求用户输入一些信息,然后将其输入文本文件,但我无法弄清楚如何读取整行中的空格。当用户输入包含空格的行时,只有第一个单词出现在文本文件中。任何人都可以帮我解决这个问题吗?在此先感谢。
这是我的计划:
#include< iostream>
#include< ; fstream>
#include< string>
使用命名空间std;
int main()
{
string line;
ofstream myfile(" playlist.txt");
if(myfile.is_open())
{
cout<< 输入艺术家:";
cin>> line;
myfile<< line<< endl;
cout<< 输入相册:;
cin>> line;
myfile<< line<< endl;
cout<< 输入曲目:;
cin>> line;
myfile<< line<< endl;
cout<< 输入歌曲:;
cin>> line;
myfile<< line<< endl;
cout<< 输入流派:";
cin>> line;
myfile<< line<< endl;
myfile.close();
}
else
cout<< 无法打开文件。;
返回0;
}
当我输入2pac for artist和Greatest Hits for album时,它跳过用户应该进入音轨的部分并转到歌曲,所以我输入了I Get Around的歌曲,它跳过了类型部分。
以下是它的作用:
输入艺术家:2pac
输入专辑:Greatest Hits
输入曲目:输入歌曲:我到处走走
输入流派:
这是输出它的文本文件:
2pac
最大
点击
我
获取
有人可以帮帮我吗?
I am making a program that asks the user to enter some information and then enters it into a text file, but I can''t figure out how to read a whole line with spaces in it. When the user enters a line with spaces in it, only the first word appears in the text file. Can anyone help me figure this out? Thanks in advance.
Here is my program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ofstream myfile("playlist.txt");
if(myfile.is_open())
{
cout << "Enter artist: ";
cin >> line;
myfile << line << endl;
cout << "Enter album: ";
cin >> line;
myfile << line << endl;
cout << "Enter track: ";
cin >> line;
myfile << line << endl;
cout << "Enter song: ";
cin >> line;
myfile << line << endl;
cout << "Enter genre: ";
cin >> line;
myfile << line << endl;
myfile.close();
}
else
cout << "Unable to open file.";
return 0;
}
When I enter 2pac for artist and Greatest Hits for album, it skips the part where the user is supposed to enter the track and goes to the song so I enter the song I Get Around, and it skips the genre part.
Here is what it does:
Enter artist: 2pac
Enter album: Greatest Hits
Enter track: Enter song: I Get Around
Enter genre:
Here is the text file it outputs it to:
2pac
Greatest
Hits
I
Get
Can somebody please help me?
你应该使用getline()函数。从输入流中取整行:
You should use the getline() function. That takes a whole line from an input stream:
展开 | 选择 | Wrap | 行号
我也使用了这样的getline函数:
I''ve also used the getline function like this:
展开 | 选择 | Wrap | 行号
您应该使用getline()函数。从输入流中取整行:
You should use the getline() function. That takes a whole line from an input stream:
展开 | 选择 | Wrap | 行号
这篇关于如何阅读包括空格在内的整行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文