求助~while中的printf
如代码中所示,在运行程序时,while循环里if语句下的printf函数能正确打印字符串;可是出了循环我再打印时(printf 'out of while'那句)就没有数据了,如此有意思的现象求解释啊~
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 50
//函数功能是读取一个文件,然后用一个char型的指针数组存储
//数组每一个元素指向从文件读取的每一行
void readLines(char *cont[])
{
FILE *fp;
int i = 0;
int j = 0;
char ch;
fp = fopen("ft_member.txt","r");
if ( fp == NULL )
{
printf("No such file: ft_member\n ");
exit(0);
}
char c[SIZE];
while ( (ch = getc(fp))!= EOF )
{
if (ch == '\n')
{
cont[j] = c;
printf("%s\n",cont[j]); //此处能正常打印cont[j]所指向的字符串
memset(c,0,SIZE);
i = 0;
j++;
}
else
{
c[i] = ch;
i++;
}
}
printf("out of while: %s\n",cont[1]); //这里就打印不出字符串了
fclose(fp);
}
int main()
{
char *lines[SIZE];
printf("read lines start....\n");
readLines(lines);
printf("read lines end....\n");
return 0;
}