Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ fstream C++;路线_C++_File_Fstream - Fatal编程技术网

C++ fstream C++;路线

C++ fstream C++;路线,c++,file,fstream,C++,File,Fstream,大家好,我很快写了一个测试程序,向大家展示我遇到的问题 我正在使用fstream将数据写入游戏文件,以便他们可以加载和保存变量 #include <iostream> #include <fstream> #include <string> int main() { std::ofstream outputFile; outputFile.open("PlayerData"); // This creates the file std::string N

大家好,我很快写了一个测试程序,向大家展示我遇到的问题

我正在使用fstream将数据写入游戏文件,以便他们可以加载和保存变量

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ofstream outputFile;
outputFile.open("PlayerData"); // This creates the file


std::string Name;
int Age;

std::cout << "Your Name: ";
std::cin >> Name;
outputFile << Name << std::endl;
std::cout << "Your Age: ";
std::cin >> Age;
outputFile << Age << std::endl;


outputFile.close();


return 0;




}
#包括
#包括
#包括
int main()
{
std::of流输出文件;
outputFile.open(“PlayerData”);//这将创建文件
std::字符串名;
智力年龄;
std::cout>Name;

outputFile如果不指定路径,则将在当前目录中创建文件,这通常不是所需的行为。您必须指定完整的路径,以确保将填充文件保存在所需的位置。如果您想知道当前可执行文件的路径,可以使用“检查”另一个问题: . 获得路径后,您可以在靠近可执行文件的路径中选择一个位置来保存数据。或者,如果愿意,可以使用系统文件夹(取决于操作系统如何在Windows检查中知道它们的名称)
永远不要将当前目录作为路径或其一部分,因为用户可以选择使用不同的当前目录运行程序。

使用文件路径中的子目录:

outputFile.Open("SubDirectory/PlayerData");
它最终出现在
Visual Studio 2013\Projects\TestFilePathing\TestFilePathing
中的原因是您在调试设置中将该目录作为工作目录。(请检查项目的配置)。 如果更改该目录(或从另一个目录的命令行启动二进制文件),则该文件将在该目录中结束


如果指定相对路径,则会出现此行为。否则,您可以指定绝对路径,无论您的工作目录是什么,文件都将始终在那里结束。

如果我使用“PlayerData”作为CharacterName变量来显示玩家之间的个性,该怎么办。std::string Jacob=“Jacob”;std::ofstream outputFile;outputFile.open(“Players”/Jacob);//这将创建文件名,在创建文件名之前先构造文件名。非常感谢,Mats!!