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

时间:2014-08-03 07:52:28

标签: 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;

}

3 个答案:

答案 0 :(得分:1)

第一个明显的问题是你需要在多行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会尝试将用户输入解析为整数而不是写cin >> count时的字符。 reply出现同样的问题。这可以通过将声明更改为char来更正。

int characters, words;
char count, reply;

现在,就在C ++中正确地将文件正确读入内存而言,请查看this answer

答案 1 :(得分:0)

首先,你可以在if语句中省略if语句中的大括号,如果&#34; if&#34;里面只会有一条指令。你有两个:

if(count=='w')
cout<<"\nEnter word to search: ";
cin>> search;

else if(count=='c')
cout<<"\nEnter character to search: ";
cin>> search;
因此编译器不会识别&#34;否则如果&#34;部分并将崩溃。添加大括号。

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
    }
}

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

答案 2 :(得分:0)

演示:我在以下代码中用于搜索整个文件中特定单词的逻辑是,如果用户输入了一个单词(比如用户输入了search =“你”),那么我们的程序将开始搜索第一个字母你的搜索词和第一个字母匹配时(在搜索[i]中i = 0)然后我们将通过递增i来检查下一个字符。如果下一个字符也匹配,那么我们将继续像之前一样递增,最后当搜索的大小等于i时(在我们的例子中,对于'你',大小= 3,所以我将被设置为0,我们将说整个单词是匹配的,所以我们在单词的计数器中递增)。同一个逻辑适用于一个字符,因为相同的逻辑,您的代码变得更容易。 IMP:对于这个逻辑,你必须关闭并重新打开文件,这样你的文件指针就会在每次读取时指向文件的开头。 (或者如果你不想重新打开文件,那么你可以在启动while(1)循环时使用'inFile.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;

}