gpt4 book ai didi

c++ - 逐个字符而不是逐个单词地读取文本文件?

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:43 25 4
gpt4 key购买 nike

我试图编写一个代码来读取名为 aisha 的文本文件

This is a new file I did it for as a trial for university
but it worked =)
Its about Removing stopwords from the file
and apply casefolding to it
It tried doing that many times
and finally now I could do now

然后代码将读取的文本存储在一个数组中,然后从中删除停用词但现在我需要折叠箱子这段代码逐字读取文本文件的问题

我想一个字符一个字符地读取它,这样我就可以对每个字符应用 casefolding有没有办法让代码逐字符读取 aisha 文件?

#include <iostream>
#include <string>
#include <fstream>

int main()
{
using namespace std;

ifstream file("aisha.txt");
if(file.is_open())
{
string myArray[200];

for(int i = 0; i < 200; ++i)
{
file >> myArray[i];

if (myArray[i] !="is" && myArray[i]!="the" && myArray[i]!="that"&& myArray[i]!="it"&& myArray[i]!="to"){
cout<< myArray[i]<<" ";
}


}
}
system("PAUSE");
return 0;
}

最佳答案

如果您将数组声明为 char 数组而不是字符串数组,提取运算符将自动读取 char。

此外,您还必须小心,因为 >> 运算符默认情况下会跳过空白字符。如果你还想阅读空格,那么你应该在阅读字符之前添加 noskipws。

file >> std::noskipws;

关于c++ - 逐个字符而不是逐个单词地读取文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20304235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com