VS 2017 檔案基本操作函式
阿新 • • 發佈:2018-12-10
Code
#include <iostream> #include <cstdlib> #include <process.h> FILE *stream, *stream1, *stream2; #pragma warning(disable:4996) int main() { int numclosed; char list[30]; // 存放從檔案中讀取的資料 int i, numread, numwritten; // 開啟檔案 data 進行度,檔案不存在則失敗 if (!(stream1 = fopen("data", "r"))) printf("Data Failed !\n"); else printf("Data Successful !\n"); // 開啟檔案2進行寫操作 if (!(stream2 = fopen("data2", "w+"))) printf("Data2 Write Failed !\n"); else printf("Data2 Write Successful !\n"); // 使用文字模式開啟檔案,對檔案進行寫操作 if ((stream = fopen("fread.out", "w+t"))) { // 向檔案流中寫入25個字元 for (i = 0; i < 25; i++) { list[i] = char('z' - i); } numwritten = fwrite(list, sizeof(char), 25, stream); printf("Written Successful: %d\n", numwritten); fclose(stream); } else { printf("fread.out Write Failed !\n"); } if ((stream = fopen("fread.out", "r"))) { numread = fread(list, sizeof(char), 25, stream); printf("讀取的資料個數 = %d\n", numread); printf("讀取的內容為:%.25s\n", list); fclose(stream); } else { printf("fread.out Read Failed !\n"); } numclosed = _fcloseall(); printf("Closed File Number is: %d\n", numclosed); system("pause"); return 0; }
Output