c ++ fileIO创建/打开文件不工作 [英] c++ fileIO creating/opening file not working
问题描述
我正在开发一个Windows VC ++ 2008项目,并尝试使用fileIO将东西放在子目录中的日志文件中。我做以下:
I am working on a Windows VC++ 2008 project, and trying to use fileIO to put stuff out in log files in a sub-directory. I am doing the following:
void MessageQueue::LogOut(thingEnum _thing){
std::ofstream Output;
Output.open("Output/MainLog.txt", std::ios::app);
if (Output.is_open()){
// writing stuff
}
Output.close();
}
$ b $ p我知道ios :: app会生成一个文件,它也能够生成文件夹,或者我需要一个不同的命令来生成一个文件夹中的文件存在。当我摆脱子目录中的代码它的工作正常,如果我创建文件夹我可以把子目录代码回来。
I know that the ios::app will generate a file, but isn't it also able to generate folders as well, or do I need a different command to generate a folder for the files to exist in. when I get rid of the sub-directory in the code it works fine, and if I create the folder I can put the sub-directory code back.
注意:我明白,我应该技术上打开文件缓冲区相同的行,我创建的流对象。我没有,因为我打算把.open放到一个case switch(_thing)以访问多个文件,只是改变流。
note: I understand that I should technically open the file buffer the same line that I create the stream object. I did not because I plan to put the .open into a case switch (_thing) to have access to multiple files, and just change the stream.
推荐答案
您可以在调用open之前使用_mkdir函数。
You could use the _mkdir function before you call open.
http://msdn.microsoft.com/en-us/library/2fkk4dzw(v = vs.90).aspx
#include <direct.h>
void MessageQueue::LogOut(thingEnum _thing){
if (_mkdir("Output\\") == 0) {
std::ofstream Output;
Output.open("Output\\MainLog.txt", std::ios::app);
if (Output.is_open()){
// writing stuff
}
Output.close();
}
else {
// could not create directory
}
}
这篇关于c ++ fileIO创建/打开文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!