使用c++计算文本文件中某些字符或单词的文件I/O
File I/O to count certain characters or words in a text file using C++
我刚开始学习c++。我已经在这个项目上工作了好几天,但没有成功。我将真诚地感谢您提供的任何建议或指导。我为任何不好的形式提前道歉,我也提前感谢你花时间帮助我。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
ifstream inFile;
string fileName, text;
char reply, enter, ch;
int character, word, counter=0;
cout<< "Please enter the file name ";
getline(cin, fileName);
//Opens the file
inFile.open(fileName.c_str());
while(!inFile)
{
cout<< "nNo such file. Enter again. ";
getline(cin, fileName);
inFile.open(fileName.c_str());
}
while(1)
{
cout<<"nCount word or character? (w/c): ";
cin>> enter;
if(enter=='w'){
cout<<"nEnter word to search: ";
cin>>text;
inFile.seekg(0, inFile.beg);
counter=0;
int i=0;
while(inFile.get(ch))
{
if(ch == ' ')
{
i=0;
}
else if(ch == text[i])
{
i++;
if(i == text.length())
{
counter++;
i=0;
}
}
}
}
else if(enter=='c')
{
cout<<"nEnter character to search: ";
cin>> text;
counter = 0;
while(inFile.get(ch))
{
if(ch == text[0])
counter++;
}
}
cout<< "nNumber of "<< text[0]<< " in file is " << counter;
cout<< "nWant to proceed this file again? (y/n) ";
cin>> reply;
if (reply == 'n')
break;
}
inFile.close();
cout<<"nThank for trying";
return 0;
}
第一个明显的问题是您需要在多行if
块的主体周围使用花括号。
if(count=='w') {
cout<<"nEnter word to search: ";
cin>> search;
}
else if(count=='c') {
cout<<"nEnter character to search: ";
cin>> search;
}
第二个问题是,您正在使用未初始化的字符缓冲区来读取用户输入。在这种情况下,最简单的是将search
更改为c++字符串。
string search;
第三个问题是您将count
声明为int
而不是char
,因此当您编写cin >> count
时,cin
将尝试将用户输入解析为整数而不是字符。同样的问题出现在reply
上。这可以通过将声明更改为char
来纠正。
int characters, words;
char count, reply;
现在,就c++中正确地将文件读入内存而言,看看这个答案。
演示:下面的代码中使用的逻辑我搜索一个特定的词在整个文件,如果用户已经进入了一个单词(比如用户输入搜索="你")那么我们计划将开始搜索你的搜索词的第一个字母,当第一个字母匹配(I = 0的搜索[我])然后我们检查下一个字符增加我。如果下一个字符也匹配然后我们像以前一样保持递增,最后当搜索的大小等于我(在我们的例子中为大小=3表示"你",所以I将被设置为0,我们会说整个单词都匹配了,所以我们为单词增加计数器)。相同的逻辑适用于一个字符,由于相同的逻辑,您的代码变得更容易。IMP:对于这种逻辑,您必须关闭并重新打开文件,以便每次读取文件时文件指针都指向文件的开头。(或者如果您不想重新打开文件,那么您可以使用' inffile '。当开始while(1)循环时,Seekg (0, is.beg);'。希望你能理解
int main(){
ifstream inFile;
string fileName, text;
char reply, enter, ch;
int character, word, counter=0;
cout<< "Please enter the file name ";
getline(cin, fileName);
//Opens the file
inFile.open(fileName.c_str());
while(!inFile)
{
cout<< "nNo such file. Enter again. ";
getline(cin, fileName);
inFile.open(fileName.c_str());
}
while(1)
{
cout<<"nCount word or character? (w/c): ";
cin>> enter;
if(enter=='w'){
cout<<"nEnter word to search: ";
cin>>text;
inFile.seekg(0, inFile.beg);
counter=0;
int i=0;
while(inFile.get(ch))
{
if(ch == ' ')
{
i=0;
}
else if(ch == text[i])
{
i++;
if(i == text.length())
{
counter++;
i=0;
}
}
}
}
else if(enter=='c')
{
cout<<"nEnter character to search: ";
cin>> text;
counter = 0;
while(inFile.get(ch))
{
if(ch == text[0])
counter++;
}
}
cout<< "nNumber of "<< text[0]<< " in file is " << counter;
cout<< "nWant to proceed this file again? (y/n) ";
cin>> reply;
if (reply == 'n')
break;
}
inFile.close();
cout<<"nThank for trying";
return 0;
}
开头,只有当"if"里面只有一条指令时,才能在if语句中提交大括号。这里有两个:
if(count=='w')
cout<<"nEnter word to search: ";
cin>> search;
else if(count=='c')
cout<<"nEnter character to search: ";
cin>> search;
因此,编译器将无法识别"else if"部分,并将崩溃。添加括号。
if(count=='w') {
cout<<"nEnter word to search: ";
cin>> search;
}
else if(count=='c') {
cout<<"nEnter character to search: ";
cin>> search;
}
下一个错误:如果用户输入错误的文件名,则程序终止。用户应该能够重新输入文件名,所以你应该在一些循环中读取文件名,例如:
ifstream f;
cout << "Enter the input file name: ";
string filename;
cin >> filename;
f.open(filename.c_str());
while (!f) {
cout << "no such file. enter again. ";
cin >> filename;
f.open(filename.c_str());
}
计算单词的逻辑示例:
string sw, ss;
cout << "type word to count: ";
cin >> sw;
int cc,cnt = 0;
while (f.good()) {
cc = f.get();
if (cc != ' ') { // if char that was read is different than space
ss += cc; // append char to string
}
else {
ss = ""; // if char is equal to space, flush the string
}
if (sw == ss) { // compare actual set of chars with given word
cnt++; // if equal, increment counter
}
}
计数字符的逻辑示例:只需在循环中读取一个字符并将其与用户给定的字符进行比较。
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 我们如何在文本文件中找到C++中的几个单词?
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 按单词搜索文件,然后在C++中打印特定行数
- 如何检查流中文件的下一个单词是否为 alpha?
- 从文件中查找单词并替换到其他文件中
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 如何在 c++ 中从文本文件中获取特定单词?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 从之前添加的批处理文件中删除单词
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- C++ 如何检查哪些单词不在 2 个类似文件中
- 显示文件中具有最小给定元音数的单词
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- 使用结构数组计算文本文件中单词的出现次数C++
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 将文本文件单词插入 AVL