#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iostream>
#include <string>
#include <math.h>
#include <direct.h>
#include <sstream>
usingnamespace std;
bool fexists(constchar *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?
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.