简体   繁体   English

fstream C ++路径

[英]fstream C++ Pathing

Hi guys so I quickly wrote a test program to show you the issue I am having. 大家好,我迅速编写了一个测试程序,向您显示我遇到的问题。

I am using fstream to write data to a file for my game so they can load and save their variables. 我正在使用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;




}

When I run the program it creates the "PlayerData" file in: 当我运行程序时,它将在以下位置创建“ PlayerData”文件:

Visual Studio 2013\\Projects\\TestFilePathing\\TestFilePathing Visual Studio 2013 \\项目\\ TestFilePathing \\ TestFilePathing

Everything works fine but I would like for instead of the PlayerData file being dumped into the TestFilePathing directory, i'd like to path it to be created into a sub directory for organization purposes. 一切正常,但我希望将PlayerData文件而不是转储到TestFilePathing目录中,我希望将其创建到子目录中以供组织使用。

Thanks :D 感谢:D

If you don't specify a path, the file will be created in the current directory, that is not normally a desired behavior. 如果您未指定路径,则文件将在当前目录中创建,这通常不是所希望的行为。 You have to specify a complete path in order to ensure the fille will be saved where you want. 您必须指定完整路径,以确保将圆角保存在所需的位置。 If you want to know the path of the current executable, you can use check this other SO question: https://stackoverflow.com/a/1528493/566608 . 如果您想知道当前可执行文件的路径,可以使用检查另一个SO问题: https : //stackoverflow.com/a/1528493/566608 Once you have the path, you can choose a location in a path near to the executable to save the data. 拥有路径后,您可以在可执行文件附近的路径中选择一个位置来保存数据。 Or, if you prefer, use system folders ( depends on the Operating System how to know their names, in Windows check this . ) Never trust current directory as a path, or part of that, because user could always choose to run your program with a different current directory. 或者,如果愿意,请使用系统文件夹(取决于操作系统如何知道其名称,在Windows中请检查 。)永远不要信任当前目录作为路径或路径的一部分,因为用户始终可以选择使用以下命令运行程序不同的当前目录。

Use the subdirectory in the file path: 使用文件路径中的子目录:

outputFile.Open("SubDirectory/PlayerData");

The reason it ends up in Visual Studio 2013\\Projects\\TestFilePathing\\TestFilePathing is that you have that directory as Working Directory in your debugging settings. 它最终出现在Visual Studio 2013\\Projects\\TestFilePathing\\TestFilePathing是,您在调试设置中将该目录作为工作目录。 (Check the configuration for the project). (检查项目的配置)。 If you change that directory (or start the binary from the command line in another directory) that file with end up in that directory. 如果更改该目录(或从另一个目录中的命令行启动二进制文件),则该文件最终将位于该目录中。

If you specify a relative path you will get this behaviour. 如果您指定相对路径,您将获得此行为。 Otherwise you can specify an absolute path and the file will end up there all the time regardless of what your working directory is. 否则,您可以指定一个绝对路径,并且无论您的工作目录是什么,文件都会一直存在该目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM