여러 줄의 문자열에서 후행 \ n을 제거한 다음 토큰에 추가하여 테이블을 나열하는 데 문제가 있습니다. 문자열은 입력 리디렉션 (< input.txt
)을 사용하여 텍스트 파일에서옵니다.문자열에서 후행 줄 바꿈을 제거하는 중 문제가 발생했습니다.
텍스트 파일은 다음과 같습니다 : 이것은 내가 지금까지 무엇을 가지고
Little Boy Blue, Come blow your horn, The sheep's in the meadow, The
cow's in the corn; Where is that boy Who looks after the sheep? Under
the haystack Fast asleep. Will you wake him? Oh no, not I, For if I do
He will surely cry.
코드 : 당신은 큰 메모리 할당 문제가
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int c;
char *line;
char *ptr;
char *pch;
line = (char *) malloc(1);
ptr = line;
for (;(*line = c = tolower(getchar())) != EOF; line++);
*line='\0';
pch = strtok(ptr," \n,.-");
while (pch != NULL)
{
printf ("%s\n", pch);
pch = strtok(NULL, " ?;,.-");
}
return 0;
}
코드가 char * 행에 1 바이트 만 할당하고 많은 코드가 (* 문자가 텍스트 파일에 있습니다. 이게 당신 문제 야? –