reading and writing

hello every body,,

i want to make some complicated operation on multiple files,,,

1- i want to create 20 files ,, each with name begin with data , then given number,, ex: "data1",, "data2",, so on,, the problem is line(31) in the code,, if i write
ofstream w("file", ios::app);
it will give my no error,, but i want to give the file name dynamicly like below,, where the filename is a string ,its value given before this line
ofstream w(filename, ios::app);


2- i want to read from single file which named "alldata",, then exemine the value inside it ,, if it is in the range ( (x1[index_value],y1[index_value]),(x2[index_value],y2[index_value]) ), if it is in this region add 1 to number_of_data[index_value],, and write this data line to the filename with this index_value,,
for example if if the px1,py1,px2,py2 is in the range of the fifth values ( (x1[4],y1[4]),(x2[4],y2[4]) ) then number_of_data[4] will add 1 to its value,, and this line ( id , px1,py1,px2,py2) will be written in file : "data4"

3- i know it is somehow complicated,, but i wish it is clear discription !!

4- this code is working ,, but with some logical buges ,, and i think there is something wrong,, after editing


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
#include <iostream>
#include <string>
#include <stdio.h>
#include <sstream>
#include <fstream>
 #include <fstream.h>
#include<fstream.h>

using namespace std;

int main() {
	double x1[20]={0, 0.5 , 1 , 1.5 ,	2 , 2.5 , 3 , 3.5 , 4 ,	4.5 , 0 , 0.5 ,	1 , 1.5 , 2 , 2.5 , 3 , 3.5,4 , 4.5 };
		double y1[20]={ 0,0,0,0,0,0,0,0,0,0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5  };
		double x2[20]={0.5,	1,1.5,2,2.5,3,3.5,4,4.5,5,0.5,1,1.5,2,2.5,3,3.5,4,4.5,	5};
		double y2[20]={0.5,	0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1,1,1,1,1,1,1};
		int number_of_data[20]={0};



	stringstream ss;
	string filename;
	ifstream fin("alldata");
	 ofstream w;             // declares stream variable called fout
	// istringstream iss(s);
// creating 20 files data1 , data2, ........, data20
	for(int i=1; i<=20;i++)
	{
	ss << "data" << i;
	filename=ss.str();
	 ofstream w(filename.c_str(), ios::app);
	    //w << i << "\n" ;//<< LastName << "\n" << Age;

	 ss.str("");
	cout<< filename<<endl;
	}
	double px1, px2, py1, py2;
	int id,op;
	while (fin)
			{
				fin >>op>>  id >> px1 >> py1 >> px2 >> py2;
				if (! fin.good()) continue; // skip newlines, etc.
			for(int f=0; f<20; f++)
			{
				if ((px1>=x1[f])&&(py1>=y1[f])&&(px2<=x2[f])&&(py2<=y2[f]) )
				{
number_of_data[f]++;
ss << "data" << f;
filename=ss.str();
w.open(filename.c_str(),ios::app);
w<<op<<"  "<<id <<"  "<<px1 <<"  "<<py1 <<"  "<<px2<<"  "<<py2<<endl;
ss.str("");
break;
				}//end if


			}//end for
	return 0;
			}
}




the code is edited


please help !!!!!!!!!!!!!!!!!
Last edited on
1: You have to use filename.c_str() to get a C string with the contents of the C++ string. This also applies to line 49
ooh it work :D
i edit the code ,, but it is terminated after reading the first line ,, this is an example for "alldata" file
1
2
3
4
5
6
7
8
9
10
11
1 0 0.427764 0.310077 0.429542 0.324097
1 1 0.919355 0.805631 0.921283 0.836945
1 2 0.0816584 0.0461462 0.159299 0.135917
1 3 0.54606 0.675024 0.635416 0.718622
1 4 0.0166278 0.721013 0.0672917 0.733018
1 5 0.947068 0.459005 0.970913 0.55668
1 6 0.68728 0.0238286 0.699067 0.113015
1 7 0.974493 0.0846899 1.05123 0.168329
1 8 0.460625 0.402921 0.520263 0.423962
1 9 0.252256 0.100032 0.309604 0.174048


the first line readed and wrote to coorect file ,, but then terminated !!!!!!!!
i solve it ,, thanks :)
the code will become like this ::
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
#include <iostream>
#include <string>
#include <stdio.h>
#include <sstream>
#include <fstream>
#include<fstream.h>

using namespace std;

int main() {
	double x1[20]={0, 0.5 , 1 , 1.5 ,	2 , 2.5 , 3 , 3.5 , 4 ,	4.5 , 0 , 0.5 ,	1 , 1.5 , 2 , 2.5 , 3 , 3.5,4 , 4.5 };
		double y1[20]={ 0,0,0,0,0,0,0,0,0,0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5  };
		double x2[20]={0.5,	1,1.5,2,2.5,3,3.5,4,4.5,5,0.5,1,1.5,2,2.5,3,3.5,4,4.5,	5};
		double y2[20]={0.5,	0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1,1,1,1,1,1,1};
		int number_of_data[20]={0};


		ofstream outfile[20];

	stringstream ss;
	string filename;
	ifstream fin("alldata");

// creating 20 files data1 , data2, ........, data20
	for(int i=0; i<20;i++)
	{
	ss << "data" << i;
	filename=ss.str();
	  outfile[i].open(filename.c_str(), ios::app);
	 ss.str("");

	cout<< filename<<endl;
	}
	double px1, px2, py1, py2;
	int id,op;
	while (fin)
			{
				fin >>op>>  id >> px1 >> py1 >> px2 >> py2;
				if (! fin.good()) continue; // skip newlines, etc.
			for(int f=0; f<20; f++)
			{
				if ((px1>=x1[f])&&(py1>=y1[f])&&(px2<=x2[f])&&(py2<=y2[f]) )
				{
number_of_data[f]++;
ss << "data" << f;
filename=ss.str();

outfile[f]<<op<<"  "<<id <<"  "<<px1 <<"  "<<py1 <<"  "<<px2<<"  "<<py2<<endl;
ss.str("");

break;
				}//end if


			}			}//end for
	return 0;
			}

Topic archived. No new replies allowed.