특정 줄 수에 따라 파일을 분할 할 수 있습니다. 그러나 특정 단어 "Turn"에서 새 파일을 쓰려고 strcmp를 사용하려고하면 그렇게 할 수 없습니다. 이 프로그램의 목표는 텍스트 파일을 읽고 "Turn"라는 단어로 새 파일을 시작하는 여러 텍스트 파일을 작성하는 것입니다. 코드에서 은 내가 strcmp와 (라인, "켜기")의 값을 인쇄하고 아무도C에서 strcmp를 사용하여 certian 키워드에서 텍스트 파일을 분할하는 방법은 무엇입니까?
#include <stdio.h>
#include <string.h>
int main() {
FILE * ptr_readfile;
FILE * ptr_writefile;
char line[128]; /* or some other suitable maximum line size */
char fileoutputname[15];
int filecounter = 1;
ptr_readfile = fopen("zion.txt", "r");
if (!ptr_readfile)
return 1;
sprintf(fileoutputname, "book_part%d", filecounter);
ptr_writefile = fopen(fileoutputname, "w");
while (fgets(line, sizeof line, ptr_readfile) != NULL) {
printf("im in the file: %s",line);
printf("VALUE %d: " , strcmp(line, "Turn"));
if(strcmp(line, "Turn") ==0) { //this cmp statment is not being met
fclose(ptr_writefile);
filecounter++;
sprintf(fileoutputname, "book_part%d", filecounter);
ptr_writefile = fopen(fileoutputname, "w");
if (!ptr_writefile)
return 1;
}
fprintf(ptr_writefile, "%s", line);
}
fclose(ptr_readfile);
return 0;
}
이 0이 출력도 반환하지 않는다 : 당신의 분할 선은 Turn
이있는 경우
im in the file: Turn
VALUE 10: im in the file: When the Lord turned again the captivity of Zion,
VALUE 3: im in the file: We were like them that dream,
VALUE 3: im in the file: We were like them that dream.
VALUE 3: im in the file: 2
VALUE -34: im in the file: Then was our mouth filled with laughter,
VALUE -13: im in the file: And our tongue with singing:
VALUE -19: im in the file: Then said they among the nations,
VALUE -13: im in the file: The Lord hath done great things,
VALUE -13: im in the file: The Lord hath done great things for them.
VALUE -13: im in the file: The Lord hath done great things for us;
VALUE -52: im in the file: Whereof we are glad.
VALUE 3: im in the file: The Lord hath done great things for us;
VALUE -13: im in the file: Whereof we are glad.
VALUE -52: im in the file: 3
VALUE -33: im in the file: Turn
VALUE 10: im in the file: again our captivity, O Lord, as the streams,
VALUE 13: im in the file: As the streams in the south,
VALUE -19: im in the file: As the streams in the south.
VALUE -19: im in the file: 4
VALUE -32: im in the file: They that sow in tears shall reap in joy,
VALUE -13: im in the file: They that sow in tears shall reap in joy.
VALUE -13: im in the file: 5
VALUE -31: im in the file: Turn
VALUE 10: im in the file: He that goeth forth and weepeth,
VALUE -12: im in the file: Bearing precious seed, shall doubtless,
VALUE -18: im in the file: Come again with rejoicing,
VALUE -17: im in the file: Come again with rejoicing,
VALUE -17: im in the file: Bringing his sheaves with him.
VALUE -18: im in the file: The Lord hath done great things for us;
VALUE -52: im in the file: Whereof we are glad.
VALUE 3: im in the file: The Lord hath done great things for us;
VALUE -13: im in the file: Whereof we are glad.
VALUE -52: im in the file: 6
VALUE -30: im in the file: Turn
VALUE 10: im in the file: When the Lord turned again the captivity of Zion,
VALUE 3: im in the file: We were like them that dream,
VALUE 3: im in the file: We were like them that dream.
VALUE 3:
'fgets()'는 읽는 줄의 끝에 줄 바꿈 문자를 포함합니다 (있는 경우). 당신은 그것을 어떻게 든 설명해야합니다 (당신이 텍스트 만 "Turn"을 가진 줄을 찾고 있다고 가정 할 때). – Dmitri
단어가 나타날 때마다 0의 값을 가져야합니다. –
예 출력을 추가하여 이해하기 쉽게했습니다. –