字符串長度沒有得到正確的長度,所以程序的其餘部分不起作用。我試圖讀取每行62個字符,然後用另外62個字符打印新行。將未初始化的字符數組傳遞給函數。崩潰
任何人都可以幫助我正確地傳遞char數組到輸出函數嗎?
#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'可能不會像你想象的那樣工作。 –
專業提示:永遠不會循環,而'feof'返回false,'EOF'標誌將不會被設置,直到* I/O操作之後。 –
另外,如果你想從一個文件中得到*行*,總是有['fgets'](http://en.cppreference.com/w/c/io/fgets)。 –