×

c语言文件读取并统计全部字符

c语言文件读取并统计全部字符(c语言编程题 通过键盘输入磁盘文件my.txt 中内容,然后再统计文件中的字符个数和行数)

admin admin 发表于2024-03-31 09:35:21 浏览23 评论0

抢沙发发表评论

各位老铁们好,相信很多人对c语言文件读取并统计全部字符都不是特别的了解,因此呢,今天就来为大家分享下关于c语言文件读取并统计全部字符以及c语言编程题 通过键盘输入磁盘文件my.txt 中内容,然后再统计文件中的字符个数和行数的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

本文目录

c语言编程题 通过键盘输入磁盘文件my.txt 中内容,然后再统计文件中的字符个数和行数

#include《stdio.h》

int main()

{ FILE *fp;

  char c;

  int n=0,n1=0;

  if((fp=fopen("my.txt","r"))==NULL)

  {printf("File open error!\n");

   retur 1;

  }

  for(;fscanf(fp,"%c",&c)==1;n++)

    if(c==’\n’)n1++; 

  printf("字符个数:%d\n行数:%d\n",n,n1);

  return 0;

}

C语言//从另一文件中读取数字 和 字符串,并进行统计

/*统计文件中重复的单词*/#include 《stdio.h》#include 《ctype.h》#include 《stdlib.h》#include 《string.h》#define FILEOPEN(x) fopen(#x,"r")#define WORD_LEN 15#define WORDS_LEN 1000int main(){   FILE *pfile=FILEOPEN(E:/2.txt);//fopen函数是第一个变元是文件路径 第二个是模式  char *parr;//记录每个单词的内存地址  int words_count;//生命记录单词的字符数组  char temp_char=0;//用于记录fgetc从文件得到的字符  int word_index=0;//记录单词的字符索引  int words_index=0;//用于记录文本的单词索引  int i,has_repeat;  if(pfile)  {  char *pcurrent_char=(char *)malloc(sizeof(char)*WORD_LEN);//分配第一个单词的内存         while(!feof(pfile))//循环一直到文件末尾     {    temp_char=fgetc(pfile);//从文件读取一个charif(isalpha(temp_char))//判断这个字符是不是字符//如果是字符就把当前的单词的字符索引位置对应的字符赋值为temp_char          pcurrent_char=temp_char;else{//如果不是字母,就认为刚才的那个单词已经结束。因为把pcurrent_char末尾添加\0pcurrent_char=’\0’;if(strlen(pcurrent_char)》0)//这里只是为了方便。防止一些空字符{     word_index=0;//把word_index设为0;用于统计下一个单词             has_repeat=0;//是否重复    for(i=0;i《words_index;i++){ //parr中寻找是否有与当前字符重复的字符,//如果有就把对应的记录单词数的数组对应的索引的值++        if(strcmp(parr,pcurrent_char)==0)//如果有两个单词相等{has_repeat=1;  words_count++;  break;}   }//如果没有重复,就重复分配内存。并且把对应统计数量的 数组索引相应位置的值为1if(!has_repeat){     parr=pcurrent_char;              words_count=1;  pcurrent_char=(char *)malloc(sizeof(char)*WORD_LEN);}      }}  } for(int i=0;i《words_index;i++)printf("%s出现%d次\t",parr);   fclose(pfile);  }  else  printf("打开文件失败\n");  return 0;}

求C语言设计一个程序,读取一个英文txt文件后统计行数、单词数、字符数和标点数,谢谢大神!

#include 《stdio.h》int main(){FILE *fp;int line=1,str,word,pu,ch;int g;if((fp=fopen("c:\\file.txt","r"))==NULL){printf("没有检测到文件\n");return;}ch=fgetc(fp);str++;while(ch!=EOF){printf("%c",ch);if(ch==’\n’){line++;}else if(ch==’ ’ || ch==’,’){ word++;if(ch==’,’){g++;}}str++;ch=fgetc(fp);}word++;printf("一共有:%d行 %d个单词 %d个字符 %d个标点符号\n",line,word,str,g);fclose(fp);}

C语言,统计文件中的字符个数

  使用fopen函数打开文件,使用fgetc()一个字符一个字符的读取,然后计数统计就可以啦,fget()从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节,这个函数的返回值,是返回所读取的一个字节。如果读到文件末尾或者读取出错时返回EOF。

#include《stdio.h》int main(){FILE *fp;char filename;int num=0;printf("输入一个文件名: ");gets(filename);if((fp=fopen(filename,"r"))==NULL){printf("文件打开失败..\n");return ;}while(!feof(fp))fgetc(fp)&&num++;printf("%s 文件中共有字符个数: %d\n",filename,num);fclose(fp);return ;}

C语言:统计一个文本文件中字母,数字及其他字符各有多少个,是编写相应程序

源代码如下:

#include《stdio.h》

#include《string.h》

void main()

{

 char str;

 int num=0,letter=0,other=0;

 int i=0;

scanf("%s",str);

for(i=0; i《strlen(str); i++)

{

if(str《=’9’) num++;

 else if(str《=’Z’) letter++;

else other++;

 }

printf("numbers: %d\nletters: %d\nothers: %d\n",num,letter,other);

}

扩展资料

1、统计文件的字符数、单词数以及总行数,包括每行的字符数和单词数。

2、空白字符(空格和tab缩进)不计入字符总数;单词以空格为分隔。不考虑一个单词在两行的情况,限制每行的字符数不能超过1000。

怎样统计一个文件中所有字符串的个数(C语言)

#include 《iostream》#include《fstream》#include《vector》using namespace std; void main() { vector《char》test; int i=0,count=1,count1=0; fstream openfile("d:\\程序\\练习专用\\hello.txt"); if(!openfile) { cout《《"open failed!"《《endl; exit(1); } do{ test.push_back(openfile.get()); count1++; if(test==’ ’) count++; if(openfile.eof()) break; i++; }while(!openfile.eof()); cout《《"the acount of the string is:"《《count《《endl; cout《《"all of them are show below:"《《endl; for(i=0;i《count1;i++) cout《《test; cout《《endl; }

c语言中怎样才能读出文件中的所有字符

用fread函数 可以整块读取。 用fgets函数 可以整行读取用fgetc函数,可以单个字符读取。 根据需求,使用对应函数,同时配合循环就可以读取整个文件了。 比如 如果fp为文件指针。那么int c;while((c = fgetc(fp)) != EOF);这样就可以遍历整个文件了。

用c语言编写一个小程序,可以读入一个英文的文本文件,显示这个文件,并统计这个文件有多少个字符,多少

#include 《stdio.h》#include 《string.h》***隐藏网址*** int main(){ FILE *fp = NULL; char read_buf = 0; int char_count = 0; int word_count = 0; int tab_count = 0; int blank_count = 0; int paragraph_count = 0; int word_start = 0; fp = fopen("English_file.txt", "r"); if (fp == NULL) { printf("Can’t open the file.\n"); return -1; } printf("The following is the file content: \n"); while (!feof(fp)) { read_buf = fgetc(fp); printf("%c", read_buf); if (isalpha(read_buf)) { char_count++;//字母 if (word_start == 0) { word_start = 1; } } else//非字母 { if (word_start == 1) { word_count++;//单词 word_start = 0; } switch (read_buf) { case ’\t’://tab tab_count++; break; case ’\040’://空格 blank_count++; break; case ’.’: case ’!’: case ’?’: case ’:’: read_buf = fgetc(fp); //再取一个字符 printf("%c", read_buf); //如果是回车或者换行就表示一个段落结束了 if ( read_buf == ’\r’ || read_buf == ’\n’ ) { paragraph_count++ ; //段落 } break; default: break; } } } printf("\n"); printf("tab_count: %d\n", tab_count); printf("blank_count: %d\n", blank_count); printf("char_count: %d\n", char_count); printf("word_count: %d\n", word_count); printf("paragraph_count: %d\n", paragraph_count); return 0;}

c语言怎么读取文件内容并且计算

主要通过fscanf,fprintf格式化输入输出函数实现,主要代码如下,#include 《stdio.h》int main(int argc, char *argv){FILE *pf=NULL;char name;//存储用户名float salary=0;//存储工资pf=fopen("test.txt", "r+" );//test.txt文件中内容为kobe 90000.80if(!pf){printf("打开文件失败,程序退出!");exit(1); } fscanf(pf,"%s %f",name,&salary);salary*=0.9;//处理工资,例如扣除五险、扣税等。 fprintf(pf,"\n%f",salary);//写入test.txt文件中 printf("%s %f\n",name,salary);//输出kobe 81000.718750if(pf)//关闭文件 {fclose( pf);pf=NULL;} return 0;}int fscanf( FILE *stream, const char *format, ... );函数fscanf()以scanf()的执行方式从给出的文件流中读取数据,fscanf()的返回值是事实上已赋值的变量的数,如果未进行任何分配时返回EOF。int fprintf( FILE *stream, const char *format, ... );fprintf()函数根据指定的format(格式)发送参数到由stream指定的文件。fprintf()只能和printf()一样工作,fprintf()的返回值是输出的字符数,发生错误时返回一个负值。

关于c语言文件读取并统计全部字符到此分享完毕,希望能帮助到您。