2014-11-25 3 views
-2

file.txt라는 파일의 단어 수를 계산하려고하지만 공백이있는 charecters를 제공합니다. 공백을 계산하지 않고 단어를 계산하는 방법은 무엇입니까?파일에서 공백을 제외한 단어 계산

#include<stdio.h> 
#include<conio.h> 
main() 
{ 

FILE *f1; 
char c; 
clrscr(); 

printf("data output"); 
f1 = fopen("file.txt","r"); 
while((c=getc(f1))!=EOF) 
{ 
    printf("%c",c); 
} 
fclose(f1); 
getch(); 
} 

되도록 빨리 해결하도록 도와주세요. 미리 감사드립니다.

+2

과 방법을 정확하게에 대한 fscanf does't 체크하면 '위의 코드에서 count'ing 때문에 어떤 단어가 100자를 초과 없다고 가정 기억 하는가? 가능한 한 빨리 알려 주시기 바랍니다. –

+0

'fgets()'를 사용하여 한 줄을 읽고'strtok()'을 사용하여 단어와 공백을 구분하십시오. – SSC

+0

나는 파일에서 단어를 세고 있었지만 공백으로 나에게 charecters를 줄 수 있도록 단어를 모른다. –

답변

1
// Create a char array to store a word 
char word[100]; 
// Stop when fscanf returns 0 
while(fscanf(f1, "%s", word)==1) 
{ 
    // print the word 
    printf("%s ",word); 
    // Increment count 
    count ++; 
} 
// Print count 
printf("%d\n", count); 

는 버퍼 오버 플로우

+0

고맙습니다. –

관련 문제