为用户定义的文件名指定路径
Specifying a path for a user defined file name
我正在尝试弄清楚如何在用户 named/defined 之后指定文件的创建位置。
我使用的教科书对此没有提供任何帮助,我试过弄乱 .open 语句中路径的定义方式,但我认为有些事情我不知道的。当我定义路径时,因为它包含在引号中,我认为文件名变量被视为字符串文字,最终创建一个名为 "filename" 的文件,而不是用户输入的任何内容。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char response;
string filename;
ofstream fileCheck1;
ifstream fileCheck;
cout << "Create file? Y(yes) or N(no)\n";
cin >> response;
if (response == 'y' || response == 'Y')
{
cout << "Please enter file name: ";
cin >> filename;
fileCheck1.open (filename);
if (fileCheck1)
cout << "file created successfully\n";
else
cout << "error opening file\n";
}
else if (response == 'n' || response == 'N')
cout << "press enter to exit\n";
else
cout << "invalid input\n";
cin.get();
cin.get();
return 0;
}
默认情况下,文件会在 visual studio.
的项目文件所在的位置创建
默认情况下,文件会在 visual studio 的项目文件所在的位置创建。因为在工作目录设置中指定了 $(ProjectDir)。考虑:
right-click on your project -> Properties -> configuration properties -> Debugging -> Working Directory
始终通过 window 顶部的下拉列表确保您是 window 中的活动配置和活动平台设置。
当您使用 Visual Studio(F5).
启动程序时,这是 Visual Studio 用作当前工作目录的路径
如果您直接从 debug/release 文件夹中 运行 .exe,它将在您当前工作目录的 .exe 文件路径中创建文件。
您可以将提到的设置更改为 $(OutputPath) 以通过 Visual Studio 通过 F5 运行 程序或通过双 -单击 .exe 文件
我正在尝试弄清楚如何在用户 named/defined 之后指定文件的创建位置。
我使用的教科书对此没有提供任何帮助,我试过弄乱 .open 语句中路径的定义方式,但我认为有些事情我不知道的。当我定义路径时,因为它包含在引号中,我认为文件名变量被视为字符串文字,最终创建一个名为 "filename" 的文件,而不是用户输入的任何内容。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char response;
string filename;
ofstream fileCheck1;
ifstream fileCheck;
cout << "Create file? Y(yes) or N(no)\n";
cin >> response;
if (response == 'y' || response == 'Y')
{
cout << "Please enter file name: ";
cin >> filename;
fileCheck1.open (filename);
if (fileCheck1)
cout << "file created successfully\n";
else
cout << "error opening file\n";
}
else if (response == 'n' || response == 'N')
cout << "press enter to exit\n";
else
cout << "invalid input\n";
cin.get();
cin.get();
return 0;
}
默认情况下,文件会在 visual studio.
的项目文件所在的位置创建默认情况下,文件会在 visual studio 的项目文件所在的位置创建。因为在工作目录设置中指定了 $(ProjectDir)。考虑:
right-click on your project -> Properties -> configuration properties -> Debugging -> Working Directory
始终通过 window 顶部的下拉列表确保您是 window 中的活动配置和活动平台设置。 当您使用 Visual Studio(F5).
启动程序时,这是 Visual Studio 用作当前工作目录的路径如果您直接从 debug/release 文件夹中 运行 .exe,它将在您当前工作目录的 .exe 文件路径中创建文件。
您可以将提到的设置更改为 $(OutputPath) 以通过 Visual Studio 通过 F5 运行 程序或通过双 -单击 .exe 文件