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;
}
|