QT ofstream使用变量作为路径名
QT ofstream use variable as a path name
我试图使一个函数需要QString以及int。将QString变量转换为ofstream的文件名,然后取整数并将其放入文件中。到目前为止,我已经设法采用一个恒定的文件名,如"filename .dat",并在其中写入一个变量。然而,当我尝试使用QString像这样:
void write(const char what,int a){
std::ofstream writefile;
writefile.open("bin\" + what);
writefile << a;
writefile.close();
}
我得到一个错误
void write(const char,int)': cannot convert argument 1 from 'const char [5]' to 'const char
这个函数调用write();
void Server::on_dial_valueChanged(int value)
{
write("dial.dat",value);
}
当我使用"bindial.dat"而不是将"bin"与字符串组合时,它工作得很好。ofstream.open ();使用"const char*"
我已经尝试了所有的文件类型,所以它们可能不匹配我的描述
问题是-有人知道如何结合"bin"和QString并使其与ofstream一起工作吗?我花了很多时间在谷歌上搜索,但仍然无法使用。谢谢!欢迎有任何建议
void write(const char what,int a)
是错误的,因为你只传递一个字符到函数,你应该有void write(const char* what,int a)
传递指针到cstring开始。
你也想连接两个cstring,在c++中你不能像在其他语言中那样做,但是你可以使用std::string来做你想做的。
试试这个
#include <string>
void write(const char* what,int a){
std::ofstream writefile;
std::string fileName("bin\");
fileName+=what;
writefile.open(fileName.c_str());
writefile << a;
writefile.close();
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 使用 c++ 从 Linux 中的 Enviornment 变量中查找文件的路径名
- QT ofstream使用变量作为路径名