【发布时间】:2016-05-26 00:52:59
【问题描述】:
我对 C++ 中的文件输入有疑问。我希望能够创建一个字符串变量并从文件中读取一个句子。我该怎么做?这是我目前的代码。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string word;
ifstream fin;
// Open the file
fin.open("file.txt");
// Read in the sentence from the file
fin >> word;
cout << word << endl;
return 0;
}
问题是,当我尝试从文件中读取一个句子时,它只读取一个单词而不是句子。
因此,如果我在文本文件中有一个句子(假设没有换行符),它只会打印出单个单词。
我该如何解决这个问题?
【问题讨论】:
-
你想读一个句子吗?还是文件中的一行?
-
如果从段落中逐句逐句,您可能需要查看 ICU。
-
显示示例输入和预期输出。
标签: c++