문자열 길이가 올바른 길이가 아니므로 나머지 프로그램이 작동하지 않습니다. 나는 한 줄에 62자를 읽고 다른 한 줄에 62자를 인쇄하려고한다.은 초기화되지 않은 문자 배열을 전달합니다. 크래킹
누구든지 출력 배열 함수에 char 배열을 올바르게 전달할 수 있습니까?
가 EOF 플래그가 후 때까지 설정되지while (!feof(getty)) { ... }
입력 작업이 실패 :
#include <stdio.h>
#include <string.h>
#define _CRT_SECURE_NO_DEPRECATE
void output(char *wbuf, char *lbuf, int lineLength);
void readFile(FILE *getty, char *wbuf, char *lbuf);
FILE *getty;
int main(void) {
char wbuf[1000] = {0}, lbuf[1000] = {0};
if (fopen_s(&getty,"getty.txt", "r") != 0)
{
printf("Failed to open getty.txt for reading.");
} else {
readFile(getty, wbuf, lbuf);
}
fclose(getty);
return 0;
}
void readFile(FILE *getty, char *wbuf, char *lbuf)
{
static int lineLength = 62;
while (!feof(getty))
{
fscanf(getty, "%s", wbuf);
output(wbuf, lbuf, lineLength);
}
}
void output(char *wbuf, char *lbuf, int lineLength)
{
int wbufLength, lbufLength, i = 0;
wbufLength = strlen(wbuf);
lbufLength = strlen(lbuf);
//prints incorrect
printf("wbuflength %d lbuflength %d\n", wbufLength, lbufLength);
// lengths
if ((wbufLength + lbufLength) <= lineLength)
{
strcat(lbuf,wbuf); //lbuf should be 0 but it starts at
} //274, wbuf not correct either
else
{
strcat(lbuf,"\n");
lineLength += 62;
strcat(lbuf, wbuf);
}
}
'feof'는 아마 당신이 생각하는 것처럼 작동하지 않습니다. –
Pro-tip : feof가 false를 반환하는 동안 반복하지 마십시오. I/O 작업 후 * EOF 플래그가 설정 될 때까지 설정되지 않습니다. –
또한 파일에서 * line *을 얻으려면 항상 ['fgets'] (http://en.cppreference.com/w/c/io/fgets)가 있습니다. –