My function is:

void Matrix::WriteToFile(std::string fname) const
    ofstream myfile; ("std::string fname.txt");
    for (int i = 0; i<data_.size(); i++)
    myfile << data_[i] << " ";

and then in the main I have A.WriteToFile("hello"), but it's saving it as fname still? Can anyone please tell me what I'm doing wrong?

Thanks in advance!

This line: ("std::string fname.txt"); is just plain silly. It'll open a file called "std::string fname.txt".
Changing it to (fname.c_str()); will probably have a more "expected" result :)
A tip: It's a good idea to check if your file is really open before writing to it.

and what is "data_"? Is it globally declared??

Edited 6 Years Ago by Nick Evan: n/a

and what is "data_"? Is it globally declared??

Thanks Nick, yes data_ is stored in a header under a private variable.

It's now saving the correct filename, however how do I specify it to a .txt file?

I tried (fname.c_str().txt); but no luck!

Fixed it with ( (fname+".txt").c_str() );

thanks both of you :)

This question has already been answered. Start a new discussion instead.