C++;在文件中保存变量 我是C++新手,我想找到一种方法,使程序存储在变量中。我不明白如何保存文件或变量,以便在下次程序打开时能够重用。例如,假设我制作了一个请求用户名并将其保存在变量中的程序,我如何才能存储该变量,以便该程序能够在以后使用时实际获取该变量?
您只需将该变量保存在一个文件中,并在下次运行程序时从该文件中读取即可 比如说,你有变量C++;在文件中保存变量 我是C++新手,我想找到一种方法,使程序存储在变量中。我不明白如何保存文件或变量,以便在下次程序打开时能够重用。例如,假设我制作了一个请求用户名并将其保存在变量中的程序,我如何才能存储该变量,以便该程序能够在以后使用时实际获取该变量?,c++,variables,C++,Variables,您只需将该变量保存在一个文件中,并在下次运行程序时从该文件中读取即可 比如说,你有变量inta将值分配给a=10。可以使用以下代码将该变量保存在文本文件中: ofstream file; file.open ("filePath.txt"); file << a; file.close(); 流文件的; file.open(“filePath.txt”); 文件您想保存变量名还是只保存变量的内容?保存为二进制还是文本? ofstream output_fil
inta代码>将值分配给a=10
。可以使用以下代码将该变量保存在文本文件中:
ofstream file;
file.open ("filePath.txt");
file << a;
file.close();
流文件的;
file.open(“filePath.txt”);
文件您想保存变量名还是只保存变量的内容?保存为二进制还是文本?
ofstream output_file( filePath );
ostream_iterator<int> output_iterator( output_file, "\n" );
// Passing all the variables inside the vector from the beginning of the vector to the end.
copy( b.begin( ), b.end( ), output_iterator );
std::vector<double> newVector;
ifstream input_file( filePath.txt );
double tempVar;
while ( input_file >> tempVar )
{
newVector.push_back( tempVar );
}