Output multiple files

Hi all,

I'm writing a simple code that will read in a number from "Numb_BBH.txt", given that number it will randomly pick said number of lines from "Realizations.txt" and I want the output to go into "Galaxy1.txt, Galaxy2.txt ...".

The first txt file is "Numb_BBH.txt" and contains the following:

2
3
6
12

The second txt file is "Realizations.txt" is 32496 lines long, the following is the first line:

7.46506616E+31 6.93184332E+31 2.68544000E+10 1.14858511E-03 2.84023559E+17 2.68544205E+10

The code that I have so far will have output files named "Galaxy2.txt, Galaxy3.txt, Galaxy6.txt, and Galaxy12.txt", but they do contain "2 lines, 3 lines, 6 lines, and 12 lines". I want the output files to be names Galaxy1.txt, Galaxy2.txt, Galaxy3.txt and Galaxy4.txt.

The following is the code I have so far. I know the problem is in line 21, but I do not know how else to name the output files.

Thanks in advance for your help :)

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

int main(){
 ifstream file("Numb_BBH.txt");
 ifstream is("Realizations.txt");
 char c;
 char d;
 int x;
 string line;
 //string fileName="Galaxy";
 srand(time(0)); //seed for randomness
 
 //while (!is.eof()){
 while (file >> x) {
  stringstream ss;
  ss<<"Galaxy"<<x<<".txt";
  ofstream file2(ss.str().c_str()); 
  for (int t = 1; t<=x; t++){
  	while (c != '\n'){
   		is.seekg(rand()%32496);
   		c = is.get();
  	}
  	getline(is,line);
  	file2 << line << endl;
  	cout << line << endl; //Printing lines randomly from file.
  	c = '0';
  }
  file2.close();
 }
 return 0;
}
Then you can use a different int variable, instead of x.

Like this:
1
2
3
4
5
6
7
8
9
10
11
int y=1;  //initialize y to 1
while (file >> x){
stringstream ss;
ss<<"Galaxy"<<y<<".txt";  //here
ofstream file2(ss.str().c_str());  
y++;  //increase y
for (int t = 1; t<=x; t++){ 
while (c != '\n'){ 
is.seekg(rand()%32496);
c = is.get(); 
}


Aceix.
Last edited on
thanks Aceix for suggestion... i needed this type of information...
Awesome thanks Aceix it works great
Hi all,

I have to process the 6th column which is the last column from the randomly picked line. So basically if the value of the chosen random line is less than 2000 then I want to keep it. The problem I'm having is that I don't know how to make the if loop continue on to print out the line. The following is the code that I want to modify. The if loop is in line 38.

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

int main(){
 ifstream file("N_testing.dat");
 //ifstream is("SpiralPeriods_TESTING.dat");
 //ifstream file("N0_populate_spiral_src.dat");
 ifstream is("SpiralPeriods0.dat");
 char c, d;
 char col1, col2, col3, col4, col5;
 int col6;
 int x;
 string line;
 //string fileName="Galaxy";
 srand(time(0)); //seed for randomness

 //while (!is.eof()){
 int y=1;
 while (file >> x) {
  stringstream ss;
  ss<<"Spiral_Galaxy_"<<y<<".txt";
  ofstream file2(ss.str().c_str());
  y++;
  for (int t = 1; t<=x; t++){
        while (c != '\n'){
                //is.seekg(rand()%32496);
                is.seekg(rand()%100008511+1);
                //is.seekg(rand()%10+1);
                c = is.get();
                //char col1, col2, col3, col4, col5;
                //int col6;
                c >> col1 >> col2 >> col3 >> col4 >> col5 >> col6;
 // Here is where I'm having trouble continuing writing the line after the if loop
                if (col6 >= 2000)
                //if (c >> col6){
                //      col6 >= 2000;
                //}

        }
        getline(is,line);
        file2 << line << endl;
        //cout << line << endl; //Printing lines randomly from file.
        c = '0';
  }
  file2.close();
 }
 return 0;
}


Thank you all very much
Topic archived. No new replies allowed.