[HELP] write to .txt

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]);
}

Hello,

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.
well, here is my programme, it stores, a few data, and it prints out what was the inputed data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>



using namespace std;



struct comp{
       string name;
       int obr;
       int bill;
       int days;
       
};


void input(comp &x){
     cout << "Name: ";
     cin >> x.name;
     cout << endl;
     cout << "Obr: ";
     cin >> x.obr;
     cout << endl;
     cout << "Bill: ";
     cin >> x.bill;
     cout << endl;
     cout << "Days: ";
     cin >> x.days;
     cout << endl; 
}

void output(comp x){
     cout << "Name: " << x.name << endl;
     cout << "Obr: " << x.obr << endl;
     cout << "Bill: " << x.bill << endl;
     cout << "Days: " << x.days << endl;
     
}

int main(){

comp *x;
int count;

cout << "Input data: " << endl;
cout <<"How many inputs?" << endl;
cin >> count;

x = new comp[count];
for (int i=0; i < count; i++)
{
input(x[i]);
output(x[i]);
}



delete [] x;
x = NULL;

system("PAUSE");
return 0;   
}


so, ye, how exactly can i write that to a file, i read quite some stuff, but nothing helped me much :S
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>
using namespace 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;
}


Also don't use system commands it's bad practice.
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. ;-)
Last edited on
Yes, i tryed, but i think i am doing something horrible wrong :S

1
2
3
4
5
6
7
8
fstream file;
file.open("BankeTXT.txt", ios::out);
for (int i=0; i < count; i++){
output(x[i]);
}


file.close();


well, the file is empty...
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.
Last edited on
I managed to make it work, but there is stil a slight "bug", if i select to enter more entries... only the last one is written to the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/function
void outputfile(comp x){
     ofstream myfile("rndfileTXT.txt");
     if(myfile.is_open()){
     myfile << "Ime izposojevalnice: " << x.ime << endl;
     myfile << "Obrestna mera: " << x.obrest << endl;
     myfile << "Znesek: " << x.znesek << endl;
     myfile << "Trajanje: " << x.trajanje << endl;
     myfile << endl; 
     myfile.close();
     }
     else cout << "Error writing";
     
}



//calling the function in main

for (int i=0; i < count; i++){
outputfile(x[i]);
}



How can i make it all be written? change the loop somehow?
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/
Yes, that is what i want to get,.. I fixed my problem, thank you for the help :)
Last edited on
Topic archived. No new replies allowed.