① c語言,統計文件中的字元個數
使用fopen函數打開文件,使用fgetc()一個字元一個字元的讀取,然後計數統計就可以啦,fget()從文件指針stream指向的文件中讀取一個字元,讀取一個位元組後,游標位置後移一個位元組,這個函數的返回值,是返回所讀取的一個位元組。如果讀到文件末尾或者讀取出錯時返回EOF。
#include<stdio.h>
intmain()
{
FILE*fp;
charfilename[50];
intnum=0;
printf("輸入一個文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("文件打開失敗.. ");
return;
}
while(!feof(fp))fgetc(fp)&&num++;
printf("%s文件中共有字元個數:%d ",filename,num);
fclose(fp);
return;
}
② C語言程序,在文件中查找指定字元串出現的次數,對文件操作實在沒寫過,看看怎麼寫
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME_MAX 50
#define SEPERATE_STRING_MAX 100
int StrCount(FILE *file,char *str);
int main()
{
char *filename,*spestr;
FILE *fp;
filename=(char *)malloc(FILE_NAME_MAX);
spestr=(char *)malloc(SEPERATE_STRING_MAX);
printf("Input the filename:");
while(1)
{
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp!=NULL)
{
break;
}
printf("Can't open the file.Try Again!");
}
printf("Input the special string:");
scanf("%s",spestr);
printf("%d times of %s in %s.",StrCount(fp,spestr),spestr,filename);
fclose(fp);
free(filename);
free(filename);
return 0;
}
int StrCount(FILE *file,char *str)
{
int count=0;
char ch;
int p=0;;
while((ch=fgetc(file))!=EOF)
{
// 當前讀入的字元匹配 str 相應位置的字元
if(ch == str[p])
{
// 匹配下一個字元
p++;
// 如果已經匹配成功
if(str[p] == '\0')
{
count++;
// 從頭開始重新匹配
p = 0;
}
}
// // 當前讀入的字元不匹配 str 相應位置的字元
else
{
// 匹配失敗,文件指針回溯
fseek(file,(p>0?-(p-1):0),SEEK_CUR);
// 從頭開始重新匹配
p = 0 ;
}
}
return count;
}
③ c語言 統計一個文本文件中字母,數字及其他字元各有多少個
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "string.h"
main()
{char a[]="abcd hgh s1&&&",ch;
int i=0,j=0,k=0,l=0;
FILE *fp;
fp=fopen("d:\\liangnv.txt","w");
fputs(a,fp);
fclose(fp);
fp=fopen("d:\\liangnv.txt","r");
ch=fgetc(fp);
while(ch!=EOF)
{ if(ch>='a'&&ch<='z')i++;
else if(ch>='A'&&ch<='Z')j++;
else if(ch>='1'&&ch<='9')k++;
else l++;
ch=fgetc(fp);}
fclose(fp);
printf("小寫字母%d\n",i);
printf("大寫字母%d\n",j);
printf("數字\n%d\n",k);
printf("字元%d\n",l);
getch();
}
④ c語言統計文件中字元的個數
while(!feof(fp)){
fgetc(fp);//最後一個收到的是文件結束符號,當然不是字元串的一部分!
num++;//而此處加了1
}
所以最後字元串的長度要減一。
而你修改後,條件先不滿足,不進入循環了,沒有num++的動作。
⑤ c語言如何統計字元個數
在C語言中,要統計一個字元串的字元個數,可以採用char類型的字元數組,再進行逐個位元組的掃描,如果它的ASCII值大於零,這個位元組算一個字元;如果它的ASCII值小於零的,就連同後續的一個位元組算一個字元。遇到ASCII值等於零,就停止統計輸出統計的結果。
⑥ C語言中如何統計一個字元文件中字元的個數我這么寫為什麼說是0個字元
打開文件模式錯誤
當前使用w+,為可讀寫模式,而且如果文件存在,會將文件重建。
要正確統計文件字元數,需要用"rb"
注意 必須加b,否則在統計換行符上會不準。
提供一種更簡單的方式:
FILE*fp=fopen(name,"rb");//這種方式,使用r或者rb都可以
fseek(fp,0,SEEK_END);//指針定義到文件結尾
intnumber=ftell(fp);//通過獲取文件指針位置,直接得到文件大小。
fclose(fp);
⑦ C語言:統計一個文本文件中字母,數字及其他字元各有多少個,是編寫相應程序
源代碼如下:
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int num=0,letter=0,other=0;
int i=0;
scanf("%s",str);
for(i=0; i<strlen(str); i++)
{
if(str[i]>='0'&&str[i]<='9') num++;
else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') letter++;
else other++;
}
printf("numbers: %d letters: %d others: %d ",num,letter,other);
}
(7)尋找目標文件的字元個數c語言擴展閱讀
1、統計文件的字元數、單詞數以及總行數,包括每行的字元數和單詞數。
2、空白字元(空格和tab縮進)不計入字元總數;單詞以空格為分隔。不考慮一個單詞在兩行的情況,限制每行的字元數不能超過1000。
⑧ 怎樣使用c語言實行將打開指定文件,並搜索其要尋找的關鍵詞個數
可以在文件打開後,使用strtok函數進行詞語的截取,然後使用strcmp函數進行關鍵詞的比較。
⑨ 怎樣統計一個文件中所有字元串的個數(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[i]==' ')
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[i];
cout<<endl;
}
⑩ 用c語言編寫一個程序,來計算文件中字元、數字的個數
#include "stdio.h"
#include "stdlib.h"
void main()
{
FILE *fp;
char ch;
int charCounts=0,numCounts=0;
if((fp=fopen("test.txt","r"))==0)//注意,由於我不知道你想打開的文件的位置和名稱,我隨意寫的一個文件名,你自己要按你文件的路徑和名稱做修改
{
printf("文件讀取失敗!\n");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
if(ch>='0'&&ch<='9')
numCounts++;
else
charCounts++;
}
printf("該文件中數字字元共有%d個,除數字這外的其它字元共有%d個。\n",numCounts,charCounts);
fclose(fp);
}