只读取文本文件中的字母
Reading in only letters from a text file
我正试图从一个文本文件中读取一首包含逗号、空格、句点和换行符的诗。我正在尝试使用getline来阅读每个单独的单词。我不想阅读任何逗号、空格、句点或换行符。当我阅读每个单词时,我会将每个字母大写,然后调用插入函数将每个单词作为一个单独的节点插入到二进制搜索树中。我不知道把每个单词分开的最好方法。我已经能够用空格分隔每个单词,但逗号、句点和换行符一直在读
这是我的文本文件:
玫瑰是红色的,小提琴是蓝色的,数据结构是最好的,你和我都知道这是真的。
我使用的代码是:
string inputFile;
cout << "What is the name of the text file?";
cin >> inputFile;
ifstream fin;
fin.open(inputFile);
//Input once
string input;
getline(fin, input, ' ');
for (int i = 0; i < input.length(); i++)
{
input[i] = toupper(input[i]);
}
//check for duplicates
if (tree.Find(input, tree.Current, tree.Parent) == true)
{
tree.Insert(input);
countNodes++;
countHeight = tree.Height(tree.Root);
}
基本上,我使用getline(fin,input,'')来读取我的输入。
我找到了一个解决方案。我能够将整行代码读入变量行,然后我搜索单词的每个字母,只保留字母,并将其存储到单词中。然后,我可以调用insert函数将Node插入到树中。
const int MAXWORDSIZE = 50;
const int MAXLINESIZE = 1000;
char word[MAXWORDSIZE], line[MAXLINESIZE];
int lineIdx, wordIdx, lineLength;
//get a line
fin.getline(line, MAXLINESIZE - 1);
lineLength = strlen(line);
while (fin)
{
for (int lineIdx = 0; lineIdx < lineLength;)
{
//skip over non-alphas, and check for end of line null terminator
while (!isalpha(line[lineIdx]) && line[lineIdx] != ' ')
++lineIdx;
//make sure not at the end of the line
if (line[lineIdx] != ' ')
{
//copy alphas to word c-string
wordIdx = 0;
while (isalpha(line[lineIdx]))
{
word[wordIdx] = toupper(line[lineIdx]);
wordIdx++;
lineIdx++;
}
//make it a c-string with the null terminator
word[wordIdx] = ' ';
//THIS IS WHERE YOU WOULD INSERT INTO THE BST OR INCREMENT FREQUENCY COUNTER IN THE NODE
if (tree.Find(word) == false)
{
tree.Insert(word);
totalNodes++;
//output word
//cout << word << endl;
}
else
{
tree.Counter();
}
}
对于我之前发布过几次的技术来说,这是一个很好的时机:定义一个ctype方面,它将除字母外的所有内容都视为空白(搜索imbue
将显示几个示例)。
从那以后,就是std::transform
的问题,istream_iterator
s在输入端,std::set
用于输出,lambda用于将第一个字母大写。
您可以为多个分隔符创建自定义getline
函数:
std::istream &getline(std::istream &is, std::string &str, std::string const& delims)
{
str.clear();
// the 3rd parameter type and the condition part on the right side of &&
// should be all that differs from std::getline
for(char c; is.get(c) && delims.find(c) == std::string::npos; )
str.push_back(c);
return is;
}
并使用它:
getline(fin, input, " n,.");
您可以使用std::regex
来选择您的代币
根据文件的大小,您可以逐行读取,也可以完全在std::string
中读取。
要读取文件,您可以使用:
std::ifstream t("file.txt");
std::string sin((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
这将对空格分隔的字符串进行匹配。
std::regex word_regex(",\s]+");
auto what =
std::sregex_iterator(sin.begin(), sin.end(), word_regex);
auto wend = std::sregex_iterator();
std::vector<std::string> v;
for (;what!=wend ; wend) {
std::smatch match = *what;
V.push_back(match.str());
}
我认为要分隔由、空格或新行分隔的标记,应该使用以下regex:(,| n| )[[:alpha:]].+
。我还没有测试,可能需要你检查一下。
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- Windows XP在C++中启动时读取文本文件的速度较慢;预取