用fopen_s替代fopen,如果用fopen会报错

来源:互联网 发布:linux装telnet包 编辑:程序博客网 时间:2024/06/06 04:19
#include "stdafx.h"
#include <Windows.h>
//#include <stdlib.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
 
 
 FILE *fp;
 char buf[21] = "c programmer";
 fopen_s(&fp,"d:\\GRADE.txt", "w"); //fopen_s一定要指定这个文件存放的路径,不能够只有文件名,不然系统找不到这个文件在哪里,也不会写字符到这个文件里面
    if (fp==NULL)                                //这个函数会清空这个文件里面所有的内容,然后写入你指定的内容.
 {
  printf("open file error!");
 }
 else
 {
  fwrite(buf, 1, 20, fp);
  fclose(fp);
 }
 system("pause");
}