【发布时间】:2017-01-09 06:18:28
【问题描述】:
这是我的代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//variable init
ifstream inFile;
ofstream outFile;
string toPrint, fileName;
string var;
cout << "Enter your save file: "; cin >> fileName;//asks the file name
cout << "Searching..."<<endl;
string fileLocation = "C:\\Users\\CraftedGaming\\Documents\\" + fileName + ".txt";//locates it
inFile.open(fileLocation.c_str());
if(!inFile){//checks if the file is existent
cerr << "Error can't find file." << endl;
outFile.open(fileLocation.c_str());
outFile << "Player House: Kubo"<<endl;
outFile.close();
}
cout << "Loaded." << endl;
inFile.ignore(1000, ':'); inFile >> var; //gets the string and places it in variable named var
cout << var<<endl;
//replaces var
cout << "Enter a string: ";
cin >> var;
//saving
outFile.open(fileLocation.c_str());
outFile << "Player House: " << var;
inFile.close();
outFile.close();
}
这里的问题是我无法将玩家的房子命名为“Kubo”并将其放在名为“var”的变量中。它设法在我的文档中创建文件并设法更改 replaces var 部分中的变量。
【问题讨论】:
-
同时打开同一个文件两次是危险的。
-
并非如此。我这样做了,对我以前的文件没有影响。此外,我没有重新打开任何文件。
-
在打开
outFile之前不要关闭inFile,也不要检查打开outFile是否成功。
标签: c++ visual-studio-2015 ifstream ofstream iomanip