Hi,
i read the writing to file on this site, but i still dont know how do do this... so i am asking for help :)
well i need to write to a .txt file what these two functions would print out in the console:
1 2 3 4
for (int i=0; i < counter; i++){
outputtext(x[i]);
values(x[i]);
}
maybe it would be useful to know what the two function do.
But writing to file is not very different from writing to the console. Instead of std::cout you can use a std::ofstream.
Just do what Mathes suggested. Instead of using cout use whatever you name your output stream object as. For example (code taken from tutorial):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// writing on a text file
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Just like I said. If you want to write to a file, you need std::ofstream.
Check http://cplusplus.com/reference/fstream/ofstream/ to know how to create such an object.
Afterwards you have two options:
1.) write a new function outputToFile that will write the data to the file (Hint: operator<<, just like for std::cout)
2.) expand the parameter list of output so it takes an std::ostream object and use the passed object for writing.
Oh, Hacaru was faster.
Btw. why do you use new[]? std::vector<comp> can be used too. It helps with the memory management. ;-)
You are not outputting anything to the file. What you are doing is creating the file. Opening the file and then displaying your contents to the console window. Then closing the file. You are not placing any text into the file. Look at the example I showed instead of cout they used myfile which in your case is just file. The reason for this Mathes has explained.
If I have understood your problem correctly your file should contain multiple forms of
Ime izposojevalnice:
Obrestna mera:
Znesek:
Trajanje:
such that the file should be like:
Ime izposojevalnice:
Obrestna mera:
Znesek:
Trajanje:
Ime izposojevalnice:
Obrestna mera:
Znesek:
Trajanje:
If this is the issue you are having use the following flag - ios::app, as it writes the data at the end of the file. All this information is available here - http://www.cplusplus.com/doc/tutorial/files/