Hi all,
I have a file with data 300 lines. (in.txt)

I want to make a program that seperates the file in two others (out1.txt, out2.txt).
But I would like to do it with percentage.

For example,
Percentage (%): 50

and seperate the in.txt file in the middle but with random lines from the file.

A line that is in the out1.txt there is no in out2.txt

Could you help me please ?

Get a random value MOD 2. If 0, output to out1.txt. If 1, out2.txt.

Simple Random Selection Without Replacement From a Finite Population

``````1. Let [b]P[/b] be the fraction of lines to be written into the first file ( [b]P[/b] == 0.63 => 63% )

2. Read all the 300 or so lines from the input file into a [b]std::vector< std::string >[/b]

3. Let [b]N[/b] be the size of the vector (number of lines read)

4. Shuffle the contents of the vector using [b]std::random_shuffle()[/b]

5. Let [b]M[/b] be [b]P * N[/b] rounded to the nearest integer

6. Write the first [b]M[/b] lines into the first output file.

7. Write the remaining [b]N-M[/b] lines into the second output file.``````

Simple Random Selection Without Replacement From a Finite Population

``````1. Let [b]P[/b] be the fraction of lines to be written into the first file ( [b]P[/b] == 0.63 => 63% )

2. Read all the 300 or so lines from the input file into a [b]std::vector< std::string >[/b]

3. Let [b]N[/b] be the size of the vector (number of lines read)

4. Shuffle the contents of the vector using [b]std::random_shuffle()[/b]

5. Let [b]M[/b] be [b]P * N[/b] rounded to the nearest integer

6. Write the first [b]M[/b] lines into the first output file.

7. Write the remaining [b]N-M[/b] lines into the second output file.``````

I found the N and read all the lines but I don't understand how to do the others
:(

``````#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

void readFile(vector<string>& name_vec, string name_file);

int main()
{
float p;
int len;

vector<string> lines;
ifstream readLines;

cout << "Fraction : ";
cin >> p;

readFile(lines, "in.txt");
len = lines.size();

ofstream outfile1("1.txt");
ofstream outfile2("2.txt");

if (!outfile1) {
cerr << "Unable to write "  << endl;
exit(1);
}

if (!outfile2) {
cerr << "Unable to write "  << endl;
exit(1);
}

cout <<"No Lines: "<< len << endl;

cout<< lines[rand()];

system("pause");
return 0;
}

void readFile(vector<string>& name_vec, string name_file)
{
ifstream infile;
infile.open(name_file.c_str());
if (!infile) {
cerr << "Unable to read " <<  name_file << endl;
exit(1);
}
for (string someName; infile >> someName; ) {
name_vec.push_back(someName);
}
infile.close();
}``````

You need to read lines (not individual strings) from the file. Use std::getline() for that.

``````std::string someName ;
while( std::getline( infile, someName ) )
name_vec.push_back(someName);``````

Then use std::random_suffle() to randomly shuffle the contents of the vector.
See: http://stdcxx.apache.org/doc/stdlibref/random-shuffle.html

Do not forget to seed the random number generator in main.
See: http://www.richelbilderbeek.nl/CppSrand.htm