다음 단어가 포함 된 파일이 있습니다.
Theendsherethiswillnotjaksdjlasdfjkl;asdjfklasdjfkl;asdfjl;
these
are
the
아래는 내 코드입니다.
int i = 0;
bool duplicateFound = false;
while(fgets(line,12,fp)){
for (int j = 0; j < i; j++){
if (strcmp(wordList[j], line) == 0){
duplicateFound = true;
printf("Duplicate Found on Line %d : %s\n", j, wordList[j]);
}
}
if (duplicateFound == false){
strcpy(wordList[i], line);
printf("%s", wordList[i]);
}
i++;*/
printf("%s", line);
}
나중에 배열에서 중복을 확인할 수 있도록 각 단어를 저장하기 위해 줄을 사용하고 있습니다. 함수가 각 줄에서 최대 12 자만 읽도록하고 싶지만 다음 출력을 출력합니다.
실제 출력 :
Theendsherethiswillnotjaksdjlasdfjkl;asdjfklasdjfkl;asdfjl;
these
are
the
예상 출력 :
Theendsheret
these
are
the
실제로 fgets를 호출 한 다음 수행해야 line[12] = '\0'
하지만 긴 줄이있는 입력을 명확하게 처리하지는 않습니다. 한 가지 옵션은 fgets가 부분 행을 반환하는 경우 (예 : strchr(line, '\n')
NULL을 반환하는 경우) 단순히 중단하는 것 입니다. 긴 줄을 처리하려면 줄 바꿈이 나타날 때까지 getchar로 데이터를 버릴 수 있습니다. 줄 바꿈을 12 자 중 하나로 간주하고 싶지 않다고 가정하면 다음과 같이 할 수 있습니다.
#include <stdio.h>
#include <string.h>
int
main(void)
{
char line[13];
while( fgets(line, 13, stdin) ) {
char *c = strchr(line, '\n');
int ch;
if( c == NULL ) while( (ch = getchar()) != EOF ) {
if( ch == '\n' ) {
break;
}
} else {
*c = '\0';
}
if( printf("%s\n", line) < 0 ) {
break;
}
}
return ferror(stdout) || ferror(stdin) || fclose(stdout) || fclose(stdin);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다