如何使用get函数从文本文件中读取大写字符,小写字符,空格字符和标点字符?

时间:2013-11-16 04:28:04

标签: file get

我正在编写一个程序来读取字母数字字符,大写字符,空格字符和标点字符,并且遇到了一些麻烦。这是我试图阅读的程序。

include iostream

include ctype.h

include stdio.h

include fstream

using namespace std;

int main(){

char ch;

int charCount = 0, upperCount = 0, punctCount = 0, whiteCount = 0;

ifstream file;

file.open("file.txt");          // open file

while (!file.eof()){            //while its not the end of the file

ch = file.get();        //get ch from the file

if (isalpha(ch)){           //if its an alphabetic character

charCount++;}           //add one to character count

else if (isupper(ch)){          //if its an uppercase letter

upperCount++;}          //add one to uppercase count

else if (ispunct(ch)){          //if its a punctuation character

punctCount++;}          //add one to punctuation count

else{                   //else its a whitespace character

whiteCount++;}          //add one to blank count

ch++;                   //increment the character

}                   //end while

file.close();               //close file

cout << "The number of alphabetic characters is: " << charCount << endl;

cout << "The number of uppercase characters is: " << upperCount << endl;

cout << "The number of punctuation characters is: " << punctCount << endl;

cout << "The number of whitespace characters is: " << whiteCount << endl;

return 0;

}                       

0 个答案:

没有答案