how do I write into many files?


Hello everybody,

I'm having problem writing into many files that Ive created.I have successfully read a file and store it as a vector. I have duplicated this file into certain number files and with diffrent names e.g. filemname1, filename2,filename3....My problem is how to write the content of the file that I've read into all these new files. I hope somebody will help me with this .
N:B: The code below is just a fractional part of the source code
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

vector <vector<point_source> > source_data;
void read_point_source(){
	ifstream source;
	source.open("../data/original_file.txt");
	if(!source.is_open()){
			cerr<<"error reading point_source file\n";
			exit(0) ;
	}
        int npoints;
	int ndata;
	source >> npoints;
	source >>ndata;
	vector<point_source> temp;
	for(int i=0;i<ndata;i++){
		point_source ts;
		temp.push_back(ts);
 source >> temp[i].index >>temp[i].year >> temp[i].month >> temp[i].day;
	}
	source_data.push_back(temp);
}
// duplicating the  file  
void duplicate_point_sources(){

	for(int i=1;i<npoints;i++){
		source_data.push_back(source_data[0]);
	}

}

/*writing the contents files */
void write_point_sourcedata(){
	    
    ofstream outfile;
	string stem="../data/newfile.txt";
    for(int i=1;i<npoints;i++){
		char tmp[20];
		itoa(i,tmp,19);
		string filename=stem+string(tmp);
		outfile.open(filename.c_str()); 
		if(!outfile.is_open()){
			cerr<<"error writing file\n";
			exit(5);
		}
// this is where Im stucked
 	outfile << 

  outfile.close();
	}

 }

closed account (S6k9GNh0)
>>.>> Not sure what you did. In order to do this is simple.
http://cplusplus.com/reference/iostream/ostream/write/

May not SEEM simple but once you understand it, it's really simple. What this does is create two file stream containers, and opens them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 infile.seekg(0,ifstream::end); //Moves towards the end of the file with an offset of 0.
  size=infile.tellg(); //Because your get pointer is at the end, you can tell how many characters are there until you reach 0. So size is the size of the file buffer.
  infile.seekg(0); //This sets the get pointer to the beginning of the file stream

  buffer = new char [size]; //Actual buffer which holds the content of the file.

  infile.read (buffer,size); //This reads the content into the buffer with the size of variable size

  // write to outfile
  outfile.write (buffer,size); //Dump all of the content of buffer into the outfile.
  
  // release dynamically-allocated memory
  delete[] buffer; //Delete the buffer which is probably taking up some space.

  outfile.close(); //Deconstruct 
  infile.close(); //Deconstruct
  return 0; //End application 



If you wondering how it knows how to format, it uses \n for enter and a couple others. When it's read into the buffer, its read as one large line with symbols like \n for formatting.
Last edited on
Topic archived. No new replies allowed.