当前位置:首页 » 编程语言 » 寻找目标文件的字符个数c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

寻找目标文件的字符个数c语言

发布时间: 2022-11-17 05:04:18

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);

}