// C10_6.cpp : Defines the entry point for the console application. // 若檔案已存在則不可覆蓋 #include "stdafx.h" #include #include #include int main(int argc, char* argv[]) { ofstream File1; // 準備對檔案進行寫入動作 int n1,n2; char Data[100]= "戰略高手" ; char strg[100]= "終極戰士" ; n1 = strlen(Data); cout << "Data 字元長度 : " << n1 << endl; n2 = strlen(strg); cout << "strg 字元長度 : " << n2 << endl; // 以 noreplace 方式開啟檔案 File1.open("File1.txt",ios::noreplace); if(!File1.is_open()) { cout << "開啟檔案失敗 , 可能檔案已存在 ! " << endl; } else { for(int i=0 ; i < 50 ; i++) { File1 << Data[i]; } File1 << endl; cout << "寫入 Data 值完成 !" << endl; for(int j=0 ; j < 50 ; j++) { File1 << strg[j]; } cout << "寫入 strg 值完成 !" << endl; } File1 << endl; File1.close(); return 0; }