Error creating multiple text files

closed account (4Gb4jE8b)
Alright let's start with the 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <stdio.h>
#include <tchar.h>
#include <fstream>

#include <iostream>
#include <string>
#include <math.h>
#include <direct.h>
#include <sstream>

using namespace std;

bool fexists(const char *filename)
{
	ifstream ifile(filename);
	return ifile;
}
int main()
{
	//analysis of parent file
	float size;
	ifstream fin;
	string File_name_opened;
	cout << "Which file? ";	//needs to check if it's a .txt
	getline(cin,File_name_opened);
	if(fexists(File_name_opened.c_str()) == false)
	{
		cout << "No file exists of that name\n";
		system("pause");
		main();
	}
	fin.open(File_name_opened.c_str());

	//tells size of parent file and number of child files
	float begin,end,number;
	begin = fin.tellg();
	fin.seekg(0, ios::end);
	end = fin.tellg();
	fin.close();
	size = (end-begin)/1000;
	cout << size << " kilobytes\n";
	number = ceil(size/4);
	if(number <= 1)
	{
		cout << "There is no need to split this file\n";
		system("pause");
		main();
	}
	cout << "There will be " << number << " files\n\n";
	cout << "If for whatever reason this is over 1000\nyou will need to add them to your iPod first 1000 at a time\n";
	cout <<	"and then the next 1000.\n\nAll files will be created regardless\n";
	system("pause");

	//creating the folder
	string Folder_name;
	cout << "\nNew directory name? ";
	getline(cin,Folder_name);
	mkdir(Folder_name.c_str());	//does not recognize "\" or "/"
	chdir(Folder_name.c_str());

	ofstream fout;
	string std_name,test;
	int x,y,z;
	x=0;y=1;z=4096;
	cout << "Base file name? ";
	getline(cin,std_name);
	for(x = 0;x<number;x++,y++)
	{
		ostringstream child_file;
		child_file << std_name << " Pt " << y << ".txt";
		test = child_file.str();
		fout.open(test.c_str());
		if(fexists(test.c_str()) == false)
		{
			cout << "\nIn file " << test.c_str() << "\n";
			cout << "Error: file unable to be made correctly\n";
			system("pause");
			return 1;
		}
	}
	system("pause");

	return 0;
}


Now what this program is supposed to do is take a txt file and split it into multiple separate files that are 4 kb each.

Now my problem comes in the for loop near the end, and i know it has something to do with the line

fout.open(test.c_str());

where I thought it would make multiple files because test.c_str() is dynamic (because child file is dynamic) it makes the first one and comes up with the error check every single time. However there are no compiler errors.

my first question is what did I do wrong and how do I fix it?
my second question is why did it go wrong? was it a type error, being that test.c_str() isn't a constant char*? If that's so why did it work in all other instances that i used it?

Thanks in advance for your help
1) You never close fout so you can't open it later.
2) No, type errors would be caught at compile time.

Also:
- Don't use system (http://www.cplusplus.com/forum/articles/11153/)
- Don't call main() (Recursion is not needed and doing this is also illegal)
closed account (4Gb4jE8b)
proof that i've been working too much on it. I've been stuck on this for two days and you smash through it like it's nothing. Thank you so so so much!

I'm working on a header file as well for replacing system commands with cin.ignore commands.

As for not calling main, how else should i loop it when a possible error occurs? I can't see a way to split the code into more functions to create simpler and smaller loops. How do you mean illegal as well, though poor form I understand.
ISO C++ forbids calling of main().
closed account (4Gb4jE8b)
good reason not to do it. But then how can i create a loop so the user doesn't need to exit every time they mistyped something??
Topic archived. No new replies allowed.