这段代码是为了打开一个文件,用fprintf输入字符串,在用fscanf返回,最后看fscanf的返回值。但是这段程序一运行就出错,猜测是指针访问问题,但是不明白原理。请大家帮忙看下,谢谢!
出错提醒是:0xCDCDCDCD 处有未经处理的异常(在 ConsoleApplication1.exe 中): 0xC0000005: 执行位置 0xCDCDCDCD 时发生访问冲突。
代码如下
#include
#include
int main(void)
{
char filename[20]="e:\abc.txt";
FILE*fp;
int n;
char str[20]="yes";
if((fp=fopen(filename,"w"))==NULL) perror("open error");
fprintf(fp,"%s",str);
n=fscanf(fp,"%s",str);
printf("%d",n);
fclose(fp);
return 0;
}