2017-04-03 171 views
-4

我希望創建幾個.txt文件來存儲有關汽車的信息。每個.txt文件將存儲一輛車的信息。我會要求用戶輸入汽車信息,例如CarId,Make,Model等。我希望使用用戶給出的CarId命名.txt文件。我怎樣才能實現這個使用C + + 11?如何使用C++創建名稱基於用戶輸入的.txt文件

謝謝!

+1

這會是很好,如果你提供你會如何填充文本文件等一些更具體的信息。 – 0xDEFACED

回答

0

你可以試試這個:

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 
int main() 
{ 
string filename; // string to take input 
ofstream out; // Object to create file 
getline(cin, filename); // Taking input for file Name 
out.open(filename + ".txt"); // To create .txt file 
out.close(); 
return 0; 
} 
+0

打開文件後沒有關閉文件。減去對此的投票。 – 0xDEFACED

+0

由於您編輯了包含「close」的答案並收回了您的投票,所以您最好刪除您的評論 – Tas

相關問題