Alan os Alan os - 1 year ago 150
C++ Question

QT ofstream use variable as a path name

I'm trying to make a function which takes QString as well as an int.
Convert QString variable into a filename for ofstream, then take the integer and place it into the file. So far I have managed to take a constant filename such as "Filename.dat" and write a variable into it. However when I try to use QString like this :

void write(const char what,int a){
std::ofstream writefile;"bin\\" + what);
writefile << a;

I get an error

void write(const char,int)': cannot convert argument 1 from 'const char [5]' to 'const char

This is the function which calls write();

void Server::on_dial_valueChanged(int value)

When I use "bin\dial.dat" instead of combining "bin\" with a string it works fine.; uses "const char*".

I've tried all the filetypes so they may not match my description

The question is-
Does anybody have an idea how to combine "bin\" and a QString and make it work with ofstream?
I've spend a lot of time googling it but still can't make it work.
Any suggestions are more than welcome

Answer Source

void write(const char what,int a) is wrong as you pass only one char to function you should have void write(const char* what,int a) to pass pointer to cstring beginning.

You also want to concat two cstrings and in c++ you can't do it like in other languages but you can use std::string to do what you want.

Try this

#include <string>

void write(const char* what,int a){
    std::ofstream writefile;
    std::string fileName("bin\\");
    writefile << a;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download