close

CRT(C執行時期函式庫)在編譯前須先設定preprocessor, 加入_CRT_SECURE_NO_WARNINGS

image

CRT安全版: 增加參數驗證, 緩衝區大小檢查, 格式化參數驗證功能

寫入fprintf_s讀取fscanf_s

   1: #include "stdafx.h"
   2: #include <cstdio>
   3: #include <iostream>
   4:  
   5: int _tmain(int argc, _TCHAR* argv[])
   6: {
   7:     // 寫入檔案
   8:     char msg[] = "我要寫入3";
   9:     FILE *fp;
  10:     printf_s("寫入檔案:\n%s\r\n", msg);
  11:     fopen_s(&fp, "test.txt", "w");
  12:     fprintf_s(fp, "%s\n", msg);
  13:     fclose(fp);
  14:  
  15:     // 讀取檔案
  16:     fopen_s(&fp, "test.txt","r");
  17:     char line[256];
  18:     fscanf_s(fp, "%s", line, 256);
  19:     printf_s("讀取檔案: \n%s\r\n", line);
  20:     fclose(fp);
  21:     system("pause");
  22:     return 0;
  23: }
  24:  
arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()