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

c语言读文件并统计字数

发布时间: 2022-07-17 03:38:47

‘壹’ c语言统计字数

在你的基础上做了点修改, 以#结束
#include<stdio.h>

int main(void)
{
int nc=0; /* number of characters */
int nq=0; /* numerical quantities */
int bs=0; /* number of blank spaces */
char e;
int nw=0,word=0,nl=1;

printf("Text Analysis\n");
printf("please input the Text:\n");
e=getchar();
while (e!='#')
{
if((e>='a'&&e<='z')||(e>='A'&&e<='Z')) nc++;
if(e>='0'&&e<='9') nq++;
if(e==' ') bs++;
if(e=='\n') nl++;

if(e==' ') word=0;
else if(word==0)
{
word=1;
nw++;
}
if(e=='\n') word=0;
else if(word==0)
{
word=1;
nw++;
}
e=getchar();
}
printf("Number of Words:%d \n",nw);
printf("Number of Characters:%d\n",nc);
printf("Number of blank spaces:%d\n",bs);
printf("Number of numerical characters:%d\n",nq);
printf("Number of lines:%d \n",nl);

return 0;
}

‘贰’ c语言统计文件中字符的个数

while(!feof(fp)){
fgetc(fp);//最后一个收到的是文件结束符号,当然不是字符串的一部分!
num++;//而此处加了1
}
所以最后字符串的长度要减一。

而你修改后,条件先不满足,不进入循环了,没有num++的动作。

‘叁’ C语言怎么样连续读取文件数据,统计单个文件的数据量

#include<iostream>
#include<fstream>
using namespace std;

#define n 3
int main()
{
int i;
char res[]="result",
suffix[]=".txt",
go_on;
for(i=1;i<=n;i++)
{
char Rname[10],Wname[20];
itoa(i,Rname,10);
strcpy(Wname,Rname);
strcat(Rname,suffix);
strcat(Wname,res);
strcat(Wname,suffix);
cout<<Rname<<':'<<endl;
ifstream infile(Rname,ios::binary);
ofstream outfile(Wname,ios::binary);
if(!infile||!outfile)
{
cerr<<"error"<<endl;
exit(1);
}
int j=0,count=0;
double d,
*dp;
cout<<"数组读取:"<<endl;

while(!infile.eof())
{
infile>>d;
count++;
}
infile.clear();
infile.seekg(0);
dp=new double[--count];
while(j<count)
{
infile>>dp[j++];
cout<<dp[j-1]<<' ';
if(j%(count/8)==0) cout<<endl;

}
cout<<endl<<"共"<<count<<"个数"<<endl;
infile.clear();
infile.seekg(0);
while(!infile.eof())
{
infile.read((char *)&d,sizeof(d));
outfile.write((char *)&d,sizeof(d));
}
delete []dp;
infile.close();
outfile.close();
cout<<"是否继续(是:y 否:anykey):";
cin>>go_on;
if(go_on!='Y'&&go_on!='y') break;
system("cls");
}
return 0;
}

‘肆’ 用.net或c语言编码,读取txt文件,并对txt文件中的字数统计个数,计算概率并排序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(Charactor)

typedef struct charactor
{
char key[3];
int count;
struct charactor *next;
}Charactor;

Charactor *add(char *c,Charactor *head) //查找记数
{
int flag=1;
Charactor *p=head;
while(p!=NULL&&flag==1)
{
if(strcmp(p->key,c) == 0)
{
flag=0; break;
}
else
{
p=p->next;
}
}

if(flag==0)
p->count++;
else
{
p=head;

head=(Charactor *)malloc(LEN);
strcpy(head->key,c);
head->count=1;
head->next=p;
}
return head;
}

void display(Charactor *p) //输出
{

printf("\n The result are:\n");
while(p!=NULL)
{

printf("%s: %d\n",p->key,p->count);
p=p->next;
}
}

void main()
{
Charactor *head=NULL;
char ch[2];
char str[100];
FILE *fstream;
FILE *fp=fopen("d:\\1.txt","w");
printf("此程序只能统计中文\n");
printf("请输入一个中文字符串\n");
fflush(stdin);
gets(str);

fprintf(fp,str); fclose(fp);
if((fstream = fopen("d:\\1.txt","r")) == NULL)
{ exit(-1); }

while( !feof(fstream) )
{ ch[0]=32;
ch[1]=32;
fscanf(fstream,"%2s",ch);
if(ch[0]==32&&ch[1]==32)
{
break;
}
head=add(ch,head);

}

display(head);

system("PAUSE");
}

c语言编码 vc6.0通过 统计您输入的字符出现的个数
觉得满意 联系我 然后我给你完善程序

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

/*
统计文件中重复的单词
*/

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#defineFILEOPEN(x)fopen(#x,"r")
#defineWORD_LEN15
#defineWORDS_LEN1000

intmain()
{

FILE*pfile=FILEOPEN(E:/2.txt);//fopen函数是第一个变元是文件路径第二个是模式

char*parr[WORDS_LEN];//记录每个单词的内存地址
intwords_count[WORDS_LEN];//生命记录单词的字符数组
chartemp_char=0;//用于记录fgetc从文件得到的字符
intword_index=0;//记录单词的字符索引
intwords_index=0;//用于记录文本的单词索引
inti,has_repeat;
if(pfile)
{
char*pcurrent_char=(char*)malloc(sizeof(char)*WORD_LEN);//分配第一个单词的内存

while(!feof(pfile))//循环一直到文件末尾
{
temp_char=fgetc(pfile);//从文件读取一个char
if(isalpha(temp_char))//判断这个字符是不是字符
//如果是字符就把当前的单词的字符索引位置对应的字符赋值为temp_char
pcurrent_char[word_index++]=temp_char;
else
{
//如果不是字母,就认为刚才的那个单词已经结束。因为把pcurrent_char末尾添加
pcurrent_char[word_index]='';
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[i],pcurrent_char)==0)//如果有两个单词相等
{
has_repeat=1;
words_count[i]++;
break;
}

}
//如果没有重复,就重复分配内存。并且把对应统计数量的数组索引相应位置的值为1
if(!has_repeat)
{
parr[words_index]=pcurrent_char;
words_count[words_index++]=1;
pcurrent_char=(char*)malloc(sizeof(char)*WORD_LEN);
}

}
}

}

for(inti=0;i<words_index;i++)
printf("%s出现%d次 ",parr[i],words_count[i]);
fclose(pfile);
}
else
printf("打开文件失败 ");

return0;
}

‘陆’ 您好,请问用c语言如何读取txt文件中的数据,并统计数据中相同数字的个数

#include <stdio.h>
void main() {
int i,j;
double data[29][34];
FILE *fp;
fp = fopen("Faults_NNA.txt","r");
for(i=0;i<29;i++) {
for(j=0;j<34;j++) {
fscanf(fp,"%6.4f",&data[i][j]);
printf("%6.4f\n",data[i][j]);
}
}
fclose(fp);
}