// C10_7.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; File1.open("File1.txt",ios::nocreate); 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; }