Ifstream 代码没有将输入放入变量中
Ifstream code is not placing the input into the variable
这是我的代码:
#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 部分中的变量。
据我了解,您需要同时读写一个文件。试试这个代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string fileName;
cout << "Enter your save file: ";
cin >> fileName;
string filePath = "C:\Users\CraftedGaming\Documents\" + fileName + ".txt";
fstream file(filePath, fstream::in | fstream::out | fstream::trunc); // open modes to read and write simultaneously
string var;
if (file.tellg() == 0)
file << "Player House: Kubo\n";
file.seekg(14);
file >> var;
cout << var << endl;
file.close();
return 0;
}
我用tellg()
判断文件是否为空,你也可以用
file.peek() == ifstream::traits_type::eof();
这是我的代码:
#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 部分中的变量。
据我了解,您需要同时读写一个文件。试试这个代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string fileName;
cout << "Enter your save file: ";
cin >> fileName;
string filePath = "C:\Users\CraftedGaming\Documents\" + fileName + ".txt";
fstream file(filePath, fstream::in | fstream::out | fstream::trunc); // open modes to read and write simultaneously
string var;
if (file.tellg() == 0)
file << "Player House: Kubo\n";
file.seekg(14);
file >> var;
cout << var << endl;
file.close();
return 0;
}
我用tellg()
判断文件是否为空,你也可以用
file.peek() == ifstream::traits_type::eof();