-
Cutting up files
Sometimes, you may want to cut up files to make them transportable by floppy disk. I'm trying to write a program to do this, but it's not working, as usual :P Here's what I've got :
Code:
void cut(char* filename, int filesizes)
{
clrscr();
fin.open(filename, ios::in | ios::nocreate);
if(!fin)
{
t("File not found. Exiting program...");
Sleep(1500);
exit(EXIT_SUCCESS);
}
string fileout = "";
int counter = 0;
int counter2 = 0;
char temp[1];
while(!fin.eof())
{
counter++;
counter2 = 0;
fin >> temp[0];
fileout += filename;
fileout += ".";
fileout += counter;
fileout += ".rps";
fout.open(fileout.c_str(), ios::out);
while(counter2 < 1024*filesizes)
{
if(fin.eof())
{
fin.close();
fout.close();
exit(EXIT_SUCCESS);
}
cout << "1, ";
fout << temp[0];
counter2++;
}
fout.close();
cout << "ONE FILE WRITTEN ! --------------------" << endl;
Sleep(1000);
}
}
As you can see, I annotated it to see what happens, but I get no files written, and I get this continuing past the EOF...
If I explain my code : the user types in a filename, and a filesize for each of the smaller files to be. This size is in kilobytes, which is why I write out 1024 times this number, as I write out a byte at a time. Void t is simply a typewriter effect for cout. It's probably something I've missed, but I can't see it.
Thanks for your help.
-
It seems like what you're looking for is what split does. Split can be found here:
http://www.gnu.org/directory/textutils.html
The source is available to you from there as well if you just want to see how they did what they did.
~SK
-
Thanks, I'll have a look at that. However, can you tell me what's wrong with my code ? Thanks.
-
Your code is a fragment and doesn't contain any comments whatsoever, so it's difficult to see what you are trying to do.
However, one thing I can see is that your "while(counter2 < 1024*filesizes)" loop never reads from the input file, which I think it's not what you want...
-
first of all, open the file with std::ios::binary, second get the file size from a call to seekg(0,std::ios::end) and tellg() third, use the file size that you got instead of the eof() call to tell you got to the end. That is all. Goodbye.