如何从文本文件中读取多个单词
How to read in multiple words from a text file?
我有一个关于 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;
}
问题是,当我尝试从文件中读取句子时,它只读取一个单词而不是句子。
因此,如果我在文本文件中有一个句子(假设没有换行符),它只会打印出单个单词。
我将如何解决这个问题?
如果你只想在'.'上拆分,那么Jason Caldwell的答案就是你要找的:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
static std::string trim(const std::string& str)
{
size_t first = str.find_first_not_of(" ntrv");
size_t last = str.find_last_not_of(" ntrv");
return str.substr(first, (last-first+1));
}
int main()
{
std::vector<std::string> sentences;
std::ifstream ifs("sentences.txt");
if (ifs.is_open() == false)
{ std::cerr << "Couldn't open file..." << std::endl; return -1; }
std::string line;
while(getline(ifs,line, '.'))
{ sentences.emplace_back(trim(line) + "."); }
for (auto sentence : sentences)
{ std::cout << "sentence: " << sentence << std::endl; }
ifs.close();
}
请注意,此代码使用 c++11 功能(auto , emplace_back...)
但是如果你假设一个句子有点复杂,我会再次建议和Jason一样,使用正则表达式。但请确保您的编译器正确实现了它们(例如:g++-4.9)
这个答案告诉你如何做到这一点。为了简单起见,您可能必须使用 std::getline 拆分字符串。
编辑:添加了对文件的检查和有关 C++11 功能的说明。
问题是,当我尝试从文件中读取句子时,它只读取一个单词而不是句子。
默认情况下,>>
提取运算符跳过空格,因此,它只提取一个单词。
要一次读取一行,可以使用 std::getline
。以下代码将读取文本文件中的第一行。您可以将getline
放入 while 循环中以逐行读取。
int main()
{
string line;
ifstream fin("file.txt");
// Read in the sentence from the file
getline(fin, line);
cout << line << 'n';
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string word; // You have to give word an actual string for this to work xd
ofstream writer("file.txt");//Creating the text document // Creating the variable that writes to it
ifstream fin("file.txt");//The variable that reads it`
if(!writer){//Checking to make sure it nothing bad occurred when opening file
cout << "An error occurred opening " << writer;
return 1; //Return an error occurred
}else{
cout << word; // Print the string word whatever it is on file.txt
}
char letter;//To get every letter that get iterated over
vector <string> textInFile(999999);//Saves letter into an array
if(!fin){
cout<<"Problem opening"<<fin; // Check if file opened safely
}else{
for(int x = 0; ! fin.eof(); x++){//eof = End of file // It basically iterates over the entire .txt;
fin.get(letter); // Gets every letter
textInFile[x] = letter; // vector stores the letters
}
for(int x = 0; x < textInFile.size(); x++){ // For size of <vector> textInFile iterate over all indexes
cout << textInFile[x]; // Prints every letter in the vector
}
}
cout<<endl;
fin.close();
return 0;
}
对不起,这个乱七八糟的帖子,这就像我的第一篇文章,我不知道这个拳击是如何工作的xd
getline
是您要查找的:http://www.cplusplus.com/reference/string/string/getline/
对于更复杂的模式匹配,正则表达式可能很有用:http://www.cplusplus.com/reference/regex/
这里发布了一个类似的问题,有很多回复:http://www.cplusplus.com/forum/general/94419/
使用getline
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
string sentence;
set <string> sentences;
ifstream file("thisfile.txt");
if(file.is_open())
{
while(getline(file, sentence, "."))
{
sentence.erase(remove_if(sentence.begin(), sentence.end(), IsChars("n")), sentence.end());
sentences.insert(sentence);
}
}
file.close(); //close file
return 0;
}
- 从字符串变量中逐字符读取单词
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 我需要C++读取字符串数组中每个单词的第一个字母
- 如何读取文件的内容,如果文件中的单词重复,请将它们添加到列表中?
- 如何读取特定单词之间的文本文件?
- 无法读取文本文件中的每个单词
- C 读单词读取文件,而没有任何符号
- C :按行读取txt文件的读数,单词用单词读取,每行的单词不均匀
- 从文件重载运算符>>读取单词
- 从文本文件(c++)中读取单词和数字
- 读取单词并将它们存储到数组中
- 如何从 std::istringstream 读取单词而不通过 >> 分配给 tmp 变量?
- 从字符串中读取单词C++,同时忽略空格、数字和符号
- 为什么我的ifstream程序以错误的顺序读取单词
- 从文件中逐字符读取单词列表,并将其存储在数组中
- 从输入中读取单词序列。使用"Quit"终止,按输入顺序打印,不要打印两次单词。
- 从一个c++文本文件中读取单词(奇怪的字符)