當前位置:首頁 » 編程語言 » 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);
}