Problem in saving results in a text file

Hello,

I have a problem with saving the result of this function:
y= 2*Sin(x) – x^2 Cos(x)
in order to draw its diagram in excel.
I want to save the result of this function when -3<=x<=+2.5, but the program just enter the last result (x=+2.5) in the text file.
I do not know how to put all of them in a file.
I would glad if anybody can give a solution with that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <fstream.h>
 #include <math.h>
 void main (void)
{

double y;
double x=-3;

   while ( x>=-3 && x<=2.5 ){
      y = (2*sin(x))-((x*x)cos(x));

      cout << x << "\t" << y << endl;
      x = x +0.1;

     ofstream file ("diagram.txt"); 
     file << x << "\t" << y << "\n";
}
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

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
    ofstream file("diagram.txt", ios::out); //create the file only once

    double y;
    double x=-3;

    while ( x>=-3 && x<=2.5 )
    {
        y = (2*sin(x))-((x*x)*cos(x));

        //cout << x << "\t" << y << endl; 
        x = x +0.1;
/*
     having the line  " ofstream file("diagram.txt"); "  in the while loop repeatedly creates and 
    destroys the file except for the last time the loop is run.
    That is why you see only x = 2.5
*/
     file << x << "\t" << y << "\n";    //edit the file as much as you want
    }
}
you are opening the file again and again and not closing it ... you should close it if you open it ....
secondly you should open the file outside the loop just once...
and you are over-writing the file ..... not just appending ...
you should use ios::append in order to get the whole data written to the file instead of writing the last one only ....
Someone has already said what I'm about to say. But to clarify, the only thing you'll have to worry about if you impliment the fixed variation of your code where the ofstream is created outside the loop, it is highly recommended you do the following:

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
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
    ofstream file("diagram.txt", ios::out); //create the file only once

    double y;
    double x=-3;

    while ( x>=-3 && x<=2.5 )
    {
        y = (2*sin(x))-((x*x)*cos(x));

        //cout << x << "\t" << y << endl; 
        x = x +0.1;
/*
     having the line  " ofstream file("diagram.txt"); "  in the while loop repeatedly creates and 
    destroys the file except for the last time the loop is run.
    That is why you see only x = 2.5
*/
     file << x << "\t" << y << "\n";    //edit the file as much as you want
    }
    file.close(); //this will prevent future potential issues with this code.
}


You may also want to eventually edit this code so as to allow to append the file instead of completely rewriting it if need be(which is what your current code will do).
Hey Gingi, i appreciate your idea to closing the file. file.close() .

I intentionally opened the file in ios::out .

The write mode overwrites the file contents only when opened. (The file is opened only once) and continues to append data to it so long as it is not closed and reopened.

Smzamir is trying to compare the variation of x and a function y(x) and this data should be new for every time the code is run and not appended).

1
2
3
4
5
6
7
8
9
10
11
12
13

.....
.....

ofstream file("diagram.txt", ios::out); //wipe all data in file

....
....

/*file is already opened and so will append this line */
file << x << "\t" << y << "\n";  
....


Topic archived. No new replies allowed.