将文件保存在不同的linux位置
Saving a file in a different place for linux
我正在尝试将文件保存到exe文件夹之外的其他位置。我把这种不合法的方式拼凑起来:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
using namespace std;
int main() {
//getting current path of the executable
char executable_path[256];
getcwd(executable_path, 255);
//unelegant back and forth conversion to add a different location
string file_loction_as_string;
file_loction_as_string = executable_path;
file_loction_as_string += "/output_files/hello_world.txt"; //folder has to exist
char *file_loction_as_char = const_cast<char*>(file_loction_as_string.c_str());
// creating, writing, closing file
ofstream output_file(file_loction_as_char);
output_file << "hello world!";
output_file.close();
}
有更优雅的方法吗?因此,不需要字符串char*。
除了mkdir
之外,是否可以在流程中创建输出文件夹?
感谢
如果使用以下代码,可以去掉3行代码。
int main()
{
//getting current path of the executable
char executable_path[256];
getcwd(executable_path, 255);
//unelegant back and forth conversion to add a different location
string file_loction_as_string = string(executable_path) + "/output_files/hello_world.txt";
// creating, writing, closing file
ofstream output_file(file_loction_as_string.c_str());
output_file << "hello world!";
output_file.close();
}
相关文章:
- 在C++/Linux中设置单调时钟的一些技巧
- Linux的Cpp上的计时器
- 将值指定给向量(2D)的向量中的某个位置
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux中哪里可以找到互斥、未来等的源代码
- 如何在linux终端中同时编译和运行c++代码
- 在Linux for Windows上编译C++代码时出错
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何找到锁定Linux futex的C++行
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- Linux 蓝牙 API 功能位置
- 将文件保存在不同的linux位置
- Linux gcc picking试图在函数所在的位置实例化一个模板
- 如何在linux中读取低级鼠标点击位置
- 如何处理不同 Linux 发行版上的不同头文件位置
- 在linux上为C++程序存储资源的位置
- 在linux终端中获取光标位置和终端大小
- 更改ld linux位置