我编写了这段代码来读取文件并对其进行分析。
我使用fgets
逐行分析它,这样我就找到了:
在最后一部分,我只分析了得到的最大一行:
感谢您的关注,我会等一些提示并帮助您找到任何错误。
#include <stdio.h>
#include <string.h>
int main(){
int i,parole,righe,paroleRiga;
char c[102],ccont,lineaMax[102], filename;
FILE * fp= fopen(filename,"r");
if (fp==NULL)
printf("errore di apertura");
parole=righe=0;
c=fgetc(fp);
lineaMax={0}
while(fgets(c,102,fp)!=NULL){
puts(c);
if(strlen(c)>strlen(lineaMax))
strncpy(lineaMax,c,strlen(c));
for (i=0;i<=strlen(c);i++)
if (c[i]!=''&&c[i]!='\n')
parole++;
}
righe++;
fclose(fp);
do{
ccont=getc(lineaMax);
if (ccont=='') paroleRiga++;
}while(ccont!='\n')
printf("numero di linee:%d,\n",righe)
print("caratteri Tot:%d\n,Max:%d,Media:%d\n",parole,paroleRiga,(parole/righe)
printf("\ntesto linea più lunga:%s",lineaMax);
return 0;
}
答案 0 :(得分:0)
#include <stdio.h>
#include <string.h>
int main(){
int i,
parole = 0, //initialize variables here, its neater
righe = 0,
paroleRiga;
char c[102], ccont, lineaMax[102] = "", *filename; //please initialize strings with strings. Btw, filename is never set
FILE * fp= fopen(filename,"r");
if (fp==NULL){
printf("errore di apertura");
exit(EXIT_FAILURE); //exit on failure
}
while(fgets(c,102,fp) != NULL){ // why 102
puts(c);
if(strlen(c) > strlen(lineaMax))
strncpy(lineaMax,c,strlen(c));
for (i=0;i<=strlen(c);i++)
if (c[i]!=' '&&c[i]!='\n')
parole++;
}
righe++;
fclose(fp);
do{
ccont=getc(lineaMax);
if (ccont==' ') paroleRiga++;
} while(ccont!='\n');
printf("numero di linee:%d,\n",righe); //righe is always 1 (0, ++)
printf("caratteri Tot:%d\n,Max:%d,Media:%d\n",parole,paroleRiga,(parole/righe);
printf("\ntesto linea più lunga:%s",lineaMax);
return EXIT_SUCCESS;
}