Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件I/O:使用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; } #包括 #包括 #包括 使用名称空间std; int main(){ 河流充填; 字符串文件名,文本; char reply,enter,ch; 整数字符,字,计数器=0; cout_C++ - Fatal编程技术网

文件I/O:使用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; } #包括 #包括 #包括 使用名称空间std; int main(){ 河流充填; 字符串文件名,文本; char reply,enter,ch; 整数字符,字,计数器=0; cout

文件I/O:使用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; } #包括 #包括 #包括 使用名称空间std; int main(){ 河流充填; 字符串文件名,文本; char reply,enter,ch; 整数字符,字,计数器=0; cout,c++,C++,第一个明显的问题是,在多行if块的主体周围需要大括号 if(count=='w') { cout<<"\nEnter word to search: "; cin>> search; } else if(count=='c') { cout<<"\nEnter character to search: "; cin>> search; } 第三个问题

第一个明显的问题是,在多行
if
块的主体周围需要大括号

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

    else if(count=='c') {
        cout<<"\nEnter character to search: ";
        cin>> search;
    }
第三个问题是,您将
count
声明为
int
而不是
char
,因此
cin
在编写
cin>>count
时会尝试将用户输入解析为整数而不是字符。
reply
也会出现同样的问题。可以通过将声明更改为
char

int characters, words;
char count, reply;

现在,在C++中正确地读取文件到内存中,先看一下

,首先,你可以在if语句中省略括号,只有当你的“if”里面只有一个指令时,这里有两个:

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

else if(count=='c')
cout<<"\nEnter character to search: ";
cin>> search;
if(计数='w')
无法搜索;
否则如果(计数='c')
无法搜索;
因此,编译器无法识别“else-if”部分并将崩溃。请添加大括号

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

else if(count=='c') {
    cout<<"\nEnter character to search: ";
    cin>> search;
}
if(计数='w'){
无法搜索;
}
否则如果(计数='c'){
无法搜索;
}
下一个错误:如果用户输入了错误的文件名,那么您的程序将终止。用户应该能够重新输入文件名,所以您应该在某个循环中读取该文件名,例如:

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());
}
iff流;
cout>文件名;
f、 打开(filename.c_str());
而(!f){
cout>文件名;
f、 打开(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
    }
}
字符串sw,ss;
cout>sw;
int-cc,cnt=0;
而(f.good()){
cc=f.get();
if(cc!=''){//if读取的字符与空格不同
ss+=cc;//将字符追加到字符串
}
否则{
ss=”“;//如果char等于空格,则刷新字符串
}
如果(sw==ss){//将实际字符集与给定单词进行比较
cnt++;//如果相等,则递增计数器
}
}

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

演示:我在以下代码中用于搜索整个文件中特定单词的逻辑是,如果用户输入了一个单词(例如user entered search=“you”)然后我们的程序将开始搜索搜索词的第一个字母,当第一个字母匹配时(在搜索[i]中i=0),我们将通过递增i来检查下一个字符。如果下一个字符也匹配,那么我们将像以前一样递增,最后当搜索的大小等于i时(在我们的例子中,“you”的大小为3,因此i将被设置为0,我们将说整个单词是匹配的,因此我们在单词的计数器中递增)。相同的逻辑用于一个字符,并且由于相同的逻辑,您的代码变得更容易。 IMP:对于这种逻辑,您必须关闭并重新打开文件,以便每次重新读取文件时,文件指针都指向文件的开头。(或者,如果您不想重新打开文件,则可以在启动while(1)循环时使用“infle.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;
intmain(){
河流充填;
字符串文件名,文本;
char reply,enter,ch;
整数字符,字,计数器=0;

可以理解问题是……?你的问题措辞非常礼貌,但是是的,如果你准确地指出问题是什么,那么会更容易帮助你。例如,你是否在出错?得到错误的结果?如果是,错误的结果是什么?嗨,问题在错误的代码部分,用来计算某些字符的数量或者用户输入的文本文件中的单词,显然存在一些逻辑错误。首先问问自己,在我用
getline(infle,str)读入
str之后,我在用
str做什么
?谢谢,现在我更明白了,但是在将我的代码编辑到您的代码后,文件无法编译..好的!您遇到了什么错误?是否有任何运行时错误?错误2错误C2228:“.length”的左侧必须有类/struct/union c:\users\avix\documents\visual studio 2010\projects\a(3)\a(3)。cpp 49这是您包含的错误之一吗“#include”并确保正确写入“search.length()”,且长度后的括号不丢失。
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;