data sending to text file !!

Hi,

my program sends data to a certain file but the program creates data for 2500 times and each time the previous number is erased and new number is saved to file. but I want all the numbers to be saved to the text file

How can I prevent the old data to be erased??
Post the code using "<>" tag, may be we will be able to help you more
closed account (DETpfSEw)
Hi!
you can solve the problem by using APPEND function because without using this function file is over write by program and cannot save previous data.
use
app();

you should check function name. hopefully it will help you.
ENJOY!!!
Last edited on
You can open the file in append mode instead of write mode: http://www.cplusplus.com/reference/iostream/fstream/fstream/
out.open("myfile.txt", ios::app);

open for append. This starts the stream at the end of the file, therefore keeping the orional data while adding new data as well.
opening it with ios:: append solved the problem but still i have another one
I'm sending the code below you dont have to go through all the code but the problem is I want this function to turn for 2500 times but when I put a for loop after defining vector dataholder and floats program doesn't work. it breaks although it works perfectly without for loop:((
<
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
65
66
67
68
69
70
71
72
73
void main()
{
	vector<vector<float>> myvec;
	vector<float>row;
	vector<vector<float>>difference;
	ifstream myfile ("C:\\Users\\dell\\Documents\\Visual Studio 2010\\Projects\\fileendvectros\\TextFile2.txt");
	float y;
	int j,i;
	for(j=0;j<2500;j++){
		row.clear();
	for(i=0;i<3;i++)
	{myfile>>y;
	row.push_back(y);
	}
	myvec.push_back(row);
	
	}
	vector<float>dataholder;
	
	float currenttemp,currentpress,currenttarget;
	int z=1;
	int l=1;
		currenttemp=myvec.at(l).at(0);
		currentpress=myvec.at(l).at(1);
		currenttarget=myvec.at(l).at(2);
		myvec.erase(myvec.begin()+l);
		vector<float>target(2);
		vector<float>easy;
		
	for(i=0;i<2499;i++)
	{
		
		target.clear();
			target.push_back(myvec.at(i).at(2));
			float d=sqrt((currenttemp-myvec.at(i).at(0))*(currenttemp-myvec.at(i).at(0))+(currentpress-myvec.at(i).at(1))*(currentpress-myvec.at(i).at(1)));
			target.push_back(d);
			easy.push_back(d);
			
			difference.push_back(target);
	}
	sort (easy.begin(),easy.end(),sorting);

	
	vector<float>nearestneighbors;
	
	for(i=0;i<z;i++)
	{
		for(int k=0;k<2499;k++)
		{
			if(difference.at(k).at(1)==easy.at(i))
				{	
					nearestneighbors.push_back(difference.at(k).at(0));
					
			break;}
		}
	}
	float sum=0;
	for(i=0;i<z;i++)
	{
		
		sum+=nearestneighbors.at(i);
		
	}
	
	
	ofstream outputfile;
	outputfile.open("C:\\Users\\dell\\Documents\\Visual Studio 2010\\Projects\\trial for file\\output.txt",ios::app);
	outputfile<<sum/z;
	outputfile.close();
	system("pause");


}


I want this function to turn for 2500 times but when I put a for loop after defining vector dataholder and floats program doesn't work. it breaks although it works perfectly without for loop
Last edited on
What breaks? it gives you compilation error? run time error? crash?
I'm sorry for not being precise I2M gonna get used with these terms. What I meant is that the main part begins(bcoz code above is the class function implementation ) it takes the data but it never ends. "press any key ..." never shows up. Another detail: I changed the above code to turn sum/z instead of saving it to a file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include"firstquestion.h"

using namespace std;

void main()
{
	FirstQuestion first;
	float expectedvalue;
	vector<float>dataholder;
	int z,l;
	cin>>z;
	//cin>>l;
		for(l=0;l<2500;l++){
	expectedvalue=first.process(l,z);
	dataholder.push_back(expectedvalue);}
	cout<<dataholder.at(0);
	system("pause");
}
First: What do you mean when you say "turn"? That makes no sense at all.

Second: Think about the steps you go through to write to a file. Try to write the code so that you have all the data of your file in your vector. Now, imagine each 'cell' of the vector as a line in that file. That is how your file will look like. Now, use a for loop. Start at 0, and end with whatever_your_vector_is_named.size(). This will loop from 0- the end of your vector. EX:

1
2
3
4
for(unsigned int x = 0; x < myvect.size(); x++)
{
    continue;
}


Now, if you want to go even further, you can add an 'if' statement to make sure you do not add an empty line to the end of your file:

1
2
3
4
5
6
7
8
9
10
out.open(file.c_str(), ios::app);
for(unsigned int x = 0; x < vector.size(); x++)
{
    out<< text;
    if(x < (myvect.size() - 1)) //if it is less than the last write-able 'cell'
    {
        out<< endl;
    }
}
out.close();


That will write each part of the vector on a new line except for the last line. So that there are no lines in the file that do not contain nothing, unless your vector conatined nothing.
Topic archived. No new replies allowed.