将文件输出到特定的文件夹C ++ Windows 7 [英] Output file to specific folder C++ Windows 7

查看:87
本文介绍了将文件输出到特定的文件夹C ++ Windows 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++并尝试输出一个文件到一个特定的地方,一个指定的名称与可执行文件在同一目录中的文件夹。找不到一个伟大的资源,在一个简单的方法来做到这一点,但我知道这一定是可能的。



我的例子。我保存一个日志文件,而不是将它保存到与可执行文件相同的目录,它保存到/ logs /



谢谢您的时间! >

编辑:我使用mkdir创建文件夹,但是如何输出到该文件夹​​。是mkdir甚至一个好东西要使用?

解决方案

此代码:

  #include< fstream> 
#include< iostream>

int main(){
std :: ofstream(C:\\mydir\\somewhere\\log.txt);
of<< hello\\\
;
}

会将hello写入目录c: \ mydir \somewhere,假设目录存在。是的,mkdir是正确的功能使用。如果你不想硬编码的路径,你可以找到路径&使用 GetModuleFileName 创建可执行文件的名称,然后创建路径请参见如何获取当前目录


I am using C++ and trying to output a file to a specific place, a folder with a specified name in the same directory as the executable. Couldn't find a great resource on an easy way to do this but I know it must be possible.

My example. I am saving a log file and instead of having it save to the same directory as the executable, it saves to /logs/

Thank you for your time!

Edit: I used mkdir to create a folder but how do I output to that folder. Is mkdir even a good thing to be using? I want to learn the best way to do this, not necessarily the easiest.

解决方案

This code:

#include <fstream>
#include <iostream>

int main()  {
    std::ofstream of( "C:\\mydir\\somewhere\\log.txt" );
    of << "hello\n";
}

will write "hello" to the file log.txt in the directory c:\mydir\somewhere, assuming the directory exists. And yes, mkdir is the right function to use. If you don't want to hardcode the path, you can find the path & name of the executable with GetModuleFileName, and then create the path programatically from that - see How to get Current Directory? for an example.

这篇关于将文件输出到特定的文件夹C ++ Windows 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆