如何计算文件中的字符,行和字 [英] How to count characters, lines, and words in a file

查看:136
本文介绍了如何计算文件中的字符,行和字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下函数计算文件中的字符和行数。

  void characterCount ifstream& inf,string fName,int length [])
{

int charNum = 0;
int lineNum = 0;

char character;

inf.get(character);
while(!inf.eof())
{
//计算结束字符计数时的行
if(character ='\\\
')
++ lineNum;

charNum ++;
inf.get(character);

if(character = EOF)
break;
}

inf.close();
inf.clear();

wordTabulate(inf,charNum,lineNum,length,fName);
}

void wordTabulate(ifstream& inf,int charNum,int lineNum,int count [],string fName)
{
inf.open(fName);
string word;
while(inf>> word)
{
cout<字;
count [word.size()] ++;
}

inf.close();
inf.clear();
inf.open(fName);

输出(inf,charNum,lineNum,count);
}

不幸的是,它只会计算1行,1个字符,基于大小的词。所有其他功能我写的它似乎工作正常。我尝试过各种不同的东西为字符计数器,但没有什么似乎工作。任何您可以提供的帮助将非常感激。



下面我添加了输出功能。我确定有一个更好的方法从数组输出,但我现在不太担心。

  void output(ifstream& inf,int charNum,int lineNum,int count [])
{
int words = 0;
cout<< Characters:< charNum< endl;

words = totalWord(count);

cout<< 单词:<词<< endl;

cout<< 段落:< lineNum< endl;

cout<< 词语分析:< endl;

cout<< 尺寸1 2 3 4 5 6 7 8 9 10+< endl;

cout<< #Words<<计数[2],计数[3],计数[4],计数[5],
计数[6],计数[7],计数[8]
}

这里是打开的文件函数,我相信是罪魁祸首。 >

  bool fileOpen(string fileName,ifstream& inf,int wordTypes [])
{
int charNum = 0;
int lineNum = 0;
cout<< File?> << endl; //请求文件名
cin>>文件名;

//打开文件并指示是否可以打开文件
bool Repeat(); $ b¥b {
inf.open(fileName.c_str());

if(inf.is_open())
{
return true;
}

if(!inf.is_open())
{
return false;
}
}
}


解决方案>

你的第一个循环中的问题是你不会在循环中读取字符,并且试图使用 eof()作为结束条件。两者都不工作。您总是需要在读取成功读取实体后检查 eof()不是您要查找的条件。您的循环应该有这样的条件:

  for(char value; in.get(value);){
...
}

上述循环将读取文件中的所有字符并避免任何需要处理 EOF 特殊。也就是说,我总是喜欢对问题的建设性解决方案。这是我的:

  void count(std :: istream& in,int& chars,int& words,int& 
{
bool word(false);
std :: for_each(std :: istreambuf_iterator< char>(in),
std :: istreambuf_iterator< char>(),
[& $ b word =!std :: isspace((++ chars,c))
&&(word || ++ words);
c =='\\\
'&& ++ lines;});
}


I'm trying to count the number of characters and lines in a file using the following function.

void characterCount(ifstream& inf, string fName, int length [])
{       

    int charNum = 0;
    int lineNum = 0;

char character;

inf.get(character);
while (!inf.eof())
{
    // counts a line when an endline character is counted
    if (character = '\n')
        ++lineNum;      

    charNum++;
    inf.get(character);

    if (character = EOF)
        break;
}

inf.close();
inf.clear();

wordTabulate(inf, charNum, lineNum, length, fName);
}

void wordTabulate(ifstream& inf, int charNum, int lineNum, int count [], string fName)
{
inf.open(fName);
string word;
while (inf >> word)
{
    cout << word;
    count[word.size()]++;
}

inf.close();
inf.clear();
inf.open(fName);

output (inf, charNum, lineNum, count);
}

Unfortunately it will only count 1 line, 1 character, and will not classify the words based on size. All the other functions I wrote for it seem to work fine. I've tried a variety of different things for the character counter but nothing seems to work. Any help you could provide would be greatly appreciated.

Below I've added the output function. I'm sure there is a better way to output from an array but I'm not too worried about that right now.

void output(ifstream& inf, int charNum, int lineNum, int count [])
{
int words = 0;
cout << "Characters: " << charNum << endl;

words = totalWord(count);

cout << "Words: " << words << endl;

cout << "Paragraphs: " << lineNum << endl;

cout << "Analysis of Words: " << endl;

cout << "Size    1 2 3 4 5 6 7 8 9 10+" << endl;

cout << "#Words" << count [2], count [3], count [4], count [5], 
                    count [6], count [7], count [8], count [9], count [10];
}

Here is the open file function which I believe is the culprit.

bool fileOpen(string fileName, ifstream& inf, int wordTypes [])
{
int charNum = 0;
int lineNum = 0;
cout << "File? >" << endl;                  // asks for file name
cin >> fileName;                            

// opens file and indicates if file can be opened
bool Repeat ();                             
{
    inf.open(fileName.c_str());  

    if (inf.is_open())
    {
        return true;
    }

    if (!inf.is_open())
    {
        return false;
    }
} 
} 

解决方案

The problem in your first loop is that you don't read a character in the loop and also that you try to use eof() as an end condition. Neither works. You always need to check after reading that you successfully read the entity and eof() is not the condition you are looking for. Your loop should have a condition like this:

for (char value; in.get(value); ) {
    ...
}

The above loop will read through all the characters in the file and avoid any need to treat EOF special. That said, I always love constructive solutions to the problem. Here is mine:

void count(std::istream& in, int& chars, int& words, int& lines)
{
    bool word(false);
    std::for_each(std::istreambuf_iterator<char>(in),
                  std::istreambuf_iterator<char>(),
                  [&](unsigned char c) {
                      word = !std::isspace((++chars, c))
                          && (word || ++words);
                      c == '\n' && ++lines; });
}

这篇关于如何计算文件中的字符,行和字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆