文件写入问题,求大神指示
#include <stdio.h>#include <stdlib.h>
#define _STDC_WANT_LIB_EXT1_
int main ()
{
long num1=234567L;
long num2=345123L;
long num3=789234L;
long num4=0L;
long num5=0L;
long num6=0L;
float fnum=0.0f;
int ival[6]={0};
FILE *pfile=NULL;
char *filename ="myfile.txt";
if(fopen_s(&pfile,filename,"w"))
{
printf("Failed to open the file.\n");
exit (1);
}
setbuf(pfile,NULL);
fprintf_s(pfile,"%6ld%6ld%6ld",num1,num2,num3);
fclose(pfile);
if(fopen_s(&pfile,filename,"r"))
{
printf("Failed to open the file");
}
setbuf(pfile,NULL);
fscanf(pfile,"%6l%6l%6l",&num4,&num5,&num6);
printf("%6ld%6ld%6ld",num4,num5,num6);
fclose (pfile);
putchar (10);
system ("pause");
return 0;
}
运行结果如下:
通过单步调试,发现在第一个fprintf函数那里出错。下面是我从百度下载的代码,进行了简单的修改:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int i=617;
char rec[100]={'\0'};
char* s = "that is a good new";
fp = fopen("D:\\text.dat","w");
fputs("total",fp);
fputs(":",fp);
fprintf(fp,"%d\n",i);
fprintf(fp,"%s",s);
rewind(fp);
fscanf(fp,"%s",rec);
printf("%s",rec);
fclose(fp);
system ("pause");
return 0;
}
编译无错误,但是运行结果不对,请见图片:
求大神解释这是怎么回事。。。