#define _CRT_SECURE_NO_DEPRECATE  /*取消scanf,printf不安全之类的错误提示*/
/* fopen example */
#include <stdio.h>
#include <string.h>
int main()
{
FILE * pFile, *pFile2;
char buf[101];
char buf2[101] = "hello file test!";
pFile = fopen("myfile.txt", "w");
pFile2 = fopen("fopentest.txt", "r"); //只读方式打开,文件必须存在。
if (pFile != NULL)
{
fputs("fopen example!", pFile);
fclose(pFile);
}
if (pFile2 != NULL){
fgets(buf, 100, pFile2);
puts(buf);
fclose(pFile2);
}
pFile2 = fopen("fopentest.txt", "a"); //附加的方式打开,插入字符到文件末尾。如果此文件不存在,则创建它。
fputs(buf2, pFile2);
fclose(pFile2);
pFile2 = fopen("fopentest.txt", "r");
fgets(buf, 100, pFile2);
puts(buf);
fclose(pFile2); pFile2 = fopen("fopentest.txt", "w");
fputs("fopen example2!", pFile2);
//fgets(buf, 100, pFile2); //向文件内写数据的时候,如果向要查看写入的数据,必须先关闭文件重新打开,否则会影响刚刚写入的数据。
fclose(pFile2);
pFile2 = fopen("fopentest.txt", "r");
fgets(buf, 100, pFile2);
fclose(pFile2);
return 0;
}

  

05-08 15:21