使用c++计算文本文件中某些字符或单词的文件I/O

File I/O to count certain characters or words in a text file using C++

本文关键字:文件 单词 字符 计算 c++ 文本 使用      更新时间:2023-10-16

我刚开始学习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
    }
}

计数字符的逻辑示例:只需在循环中读取一个字符并将其与用户给定的字符进行比较。