Splitting file in mulitple files

Write your question here.
I am trying to create a file splitter that reads a file and split it into three files (one.txt, two.txt and three.txt) with five records each but when I run this I get weird filesnames with incorrect or no content.

Can anybody have a look and tell what am I doing wrong?


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

int main ()
{
    string file_line;
    ifstream infile;
	ofstream outfiles[100];
	char *targetfilename;
	int number_of_lines;
	infile.open("source.txt");
	char filenames[3][10] = {"one.txt","two.txt","three.txt"};
	
    int line_counter = 0;
	int file_counter=1;
	
	while (getline(infile, file_line))
	{
		outfiles[file_counter].open(filenames[file_counter]);
		while (line_counter <= 5)
			{
			outfiles[file_counter] << file_line << endl;
			line_counter++;
			}
		outfiles[file_counter].close();
		file_counter++;	   	   	   
	}
    infile.close();
	system ("pause");
}
Without having tried this, you need to set line_counter = 0 again at the end of the outermost while-loop. Also, file-counter should be initialized to 0.
Last edited on
Topic archived. No new replies allowed.