C++ 阅读'';和'';作为.txt文件中的空格

C++ 阅读'';和'';作为.txt文件中的空格,c++,count,C++,Count,我需要帮助读取.txt文件中的逗号和句点作为空格。该程序运行平稳,输出正确的字数和数字,但我需要有句点和逗号的数字作为单独的数字读取 #include <iostream> #include <iterator> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main(int argc, char* argv[]) {

我需要帮助读取.txt文件中的逗号和句点作为空格。该程序运行平稳,输出正确的字数和数字,但我需要有句点和逗号的数字作为单独的数字读取

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word)            // read the text file word-by-word
    {
        if (isdigit(word.at(0)))    // if the first letter a digit it counts as number
        {
            countNumbers++;
        }
        else
        {
            countWords++;
        }
        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
int countWords=0,countNumbers=0;
ifstream文件;
file.open(“input1.txt”);
字符串字;
while(file>>word)//逐字读取文本文件
{
if(isdigit(word.at(0))//如果第一个字母是一个数字,则它算作数字
{
countNumbers++;
}
其他的
{
countWords++;
}

cout这是您新修改的问题的一个解决方案。我采用的方法是遍历每个字符串并计算数字或单词,考虑到句点和逗号应被视为空格。在您的例子中,我假设给定的单词不能由字母或数字的组合组成。换句话说,如果如果一个字符是一个数字,那么整个单词就是一个数字,否则它就是一个字符单词

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void countWord(const string& word, int &countNumbers, int &countWords);

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word) {
        // iterate over each "word"
        countWord(word, countNumbers, countWords);

        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}

void countWord(const string &word, int &countNumbers, int &countWords) {
    bool word_start = true;

    for (int i=0; i < word.length(); i++) {
        if (word_start == true) {
            if (word.at(i) == '.' || word.at(i) == ',') {
                continue;
            }
            else if (isdigit(word.at(i)) {
                countNumbers++;
            }
            else {
                countWords++;
            }
            word_start = false;
        }

        if (word.at(i) == '.' || word.at(i) == ',') {
            word_start = true;
        }
    }

    return;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void countWord(常量字符串和单词、int和countNumbers、int和countWords);
int main(int argc,char*argv[]){
int countWords=0,countNumbers=0;
ifstream文件;
file.open(“input1.txt”);
字符串字;
while(文件>>word){
//迭代每个“单词”
countWord(单词、countnumber、countWords);

cout这是您新修改的问题的一个解决方案。我采用的方法是遍历每个字符串并计算数字或单词,考虑到句点和逗号应被视为空格。在您的例子中,我假设给定的单词不能由字母或数字的组合组成。换句话说,如果如果一个字符是一个数字,那么整个单词就是一个数字,否则它就是一个字符单词

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void countWord(const string& word, int &countNumbers, int &countWords);

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word) {
        // iterate over each "word"
        countWord(word, countNumbers, countWords);

        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}

void countWord(const string &word, int &countNumbers, int &countWords) {
    bool word_start = true;

    for (int i=0; i < word.length(); i++) {
        if (word_start == true) {
            if (word.at(i) == '.' || word.at(i) == ',') {
                continue;
            }
            else if (isdigit(word.at(i)) {
                countNumbers++;
            }
            else {
                countWords++;
            }
            word_start = false;
        }

        if (word.at(i) == '.' || word.at(i) == ',') {
            word_start = true;
        }
    }

    return;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void countWord(常量字符串和单词、int和countNumbers、int和countWords);
int main(int argc,char*argv[]){
int countWords=0,countNumbers=0;
ifstream文件;
file.open(“input1.txt”);
字符串字;
while(文件>>word){
//迭代每个“单词”
countWord(单词、countnumber、countWords);

cout这是您新修改的问题的一个解决方案。我采用的方法是遍历每个字符串并计算数字或单词,考虑到句点和逗号应被视为空格。在您的例子中,我假设给定的单词不能由字母或数字的组合组成。换句话说,如果如果一个字符是一个数字,那么整个单词就是一个数字,否则它就是一个字符单词

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void countWord(const string& word, int &countNumbers, int &countWords);

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word) {
        // iterate over each "word"
        countWord(word, countNumbers, countWords);

        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}

void countWord(const string &word, int &countNumbers, int &countWords) {
    bool word_start = true;

    for (int i=0; i < word.length(); i++) {
        if (word_start == true) {
            if (word.at(i) == '.' || word.at(i) == ',') {
                continue;
            }
            else if (isdigit(word.at(i)) {
                countNumbers++;
            }
            else {
                countWords++;
            }
            word_start = false;
        }

        if (word.at(i) == '.' || word.at(i) == ',') {
            word_start = true;
        }
    }

    return;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void countWord(常量字符串和单词、int和countNumbers、int和countWords);
int main(int argc,char*argv[]){
int countWords=0,countNumbers=0;
ifstream文件;
file.open(“input1.txt”);
字符串字;
while(文件>>word){
//迭代每个“单词”
countWord(单词、countnumber、countWords);

cout这是您新修改的问题的一个解决方案。我采用的方法是遍历每个字符串并计算数字或单词,考虑到句点和逗号应被视为空格。在您的例子中,我假设给定的单词不能由字母或数字的组合组成。换句话说,如果如果一个字符是一个数字,那么整个单词就是一个数字,否则它就是一个字符单词

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void countWord(const string& word, int &countNumbers, int &countWords);

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word) {
        // iterate over each "word"
        countWord(word, countNumbers, countWords);

        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}

void countWord(const string &word, int &countNumbers, int &countWords) {
    bool word_start = true;

    for (int i=0; i < word.length(); i++) {
        if (word_start == true) {
            if (word.at(i) == '.' || word.at(i) == ',') {
                continue;
            }
            else if (isdigit(word.at(i)) {
                countNumbers++;
            }
            else {
                countWords++;
            }
            word_start = false;
        }

        if (word.at(i) == '.' || word.at(i) == ',') {
            word_start = true;
        }
    }

    return;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void countWord(常量字符串和单词、int和countNumbers、int和countWords);
int main(int argc,char*argv[]){
int countWords=0,countNumbers=0;
ifstream文件;
file.open(“input1.txt”);
字符串字;
while(文件>>word){
//迭代每个“单词”
countWord(单词、countnumber、countWords);

cout不必逐字阅读,你可以阅读所有的行并逐字重复。因此,当你找到一个空格、点或逗号时,你可以检查它是一个单词还是一个数字

因此,您可以使用函数getline读取一行并迭代所有字符。代码如下所示:

while (getline(file, line))
{
    string currentWord;
    char currentChar;
    for (int i = 0; i < line.length(); i++)
    {
        currentChar = line[i];
        if (currentChar == ' ' || currentChar == ',' || currentChar == '.')
        {
            if (currentWord != "")
            {
                // check if currentWord is composed by letters or numbers here
            }
            currentWord = "";
        }
        else
        {
            currentWord += currentChar;
        }
    }
}
while(getline(文件,行))
{
字符串当前字;
字符当前字符;
对于(int i=0;i
也许您还需要检查currentChar是否与“\r”或“\n”不同。另一个选项是检查currentChar是否既不是字母也不是数字


此外,请记住在读取文件后关闭它。

您可以逐字读取所有行并逐字符迭代。因此,当您找到空白、点或逗号时,您可以检查它是单词还是数字

因此,您可以使用函数getline读取一行并迭代所有字符。代码如下所示:

while (getline(file, line))
{
    string currentWord;
    char currentChar;
    for (int i = 0; i < line.length(); i++)
    {
        currentChar = line[i];
        if (currentChar == ' ' || currentChar == ',' || currentChar == '.')
        {
            if (currentWord != "")
            {
                // check if currentWord is composed by letters or numbers here
            }
            currentWord = "";
        }
        else
        {
            currentWord += currentChar;
        }
    }
}
while(getline(文件,行))
{
字符串当前字;
字符当前字符;
对于(int i=0;i