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

File 如何使用get函数从文本文件中读取大写字符、小写字符、空格字符和标点符号?,file,get,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

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

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;

}                       
包含iostream
包括ctype.h
包括stdio.h
包括fstream
使用名称空间std;
int main(){
char ch;
int charCount=0,upperCount=0,punctCount=0,whiteCount=0;
ifstream文件;
file.open(“file.txt”);//打开文件
while(!file.eof()){//而它不是文件的结尾
ch=file.get();//从文件中获取ch
if(isalpha(ch)){//if是字母字符
charCount++;}//将一个字符添加到字符计数
else if(isupper(ch)){//如果是大写字母
upperCount++;}//将一个添加到大写计数
else if(ispunct(ch)){//如果是标点符号
punctCount++;}//将一个添加到标点计数
else{//else是一个空白字符
whiteCount++;}//将一个添加到空白计数
ch++;//递增字符
}//结束时
file.close();//关闭文件

编写更好的问题:在编写问题时,您可以使用文本字段顶部的花括号图标来编写代码。这使阅读代码变得更加容易。对于问题本身:首先,它是否编译?如果是,是否实际得到文件?我注意到您没有放菱形括号()围绕你的内容,这可能是个问题。你可以考虑添加标签<代码> C++ >代码>,这样你就可以吸引更多的用户进入页面。