Writing string to c++ file (QT4)

69 Views Asked by At

I just need to write a string into a file created using ofstream, but I am getting an error.

This is the code:

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    QString aux = "Hello";
    ofstream myfile ("test.txt");

    if (myfile.is_open())
    {
        myfile << aux;
        myfile.close();
    }
    else
    {
        cout << "CANT OPEN FILE";
    }
    return 0;

}

The error is: no match for 'operator<<' in 'myfile << aux'

P.S: I am using QT4

Thanks for your help!

1

There are 1 best solutions below

0
On BEST ANSWER

You should convert to a string by doing : myfile << aux.toStdString(); This is because the << operator does not know any conversion from qt string.