in the fin.read() function it is saying that it needs a "char*_Str" and a stringsize count. I have the count down, but how would I accurately initialize a "char*_Str" that can then be passed into fout as a string?
You're trying to read from a file into a string, you can't do it that way. If it's a text file (as opposed to a binary file), it's normal to use getline to read an entire line into a string.
For example:
1 2 3 4
string line;
ifstream fin(inputfile.c_str());
while (getline(fin, line))
cout << line << std::endl;
That's fine, grab everything and load it into memory first. THEN parse through the data and pull out what you need.
People say that parsing data is trickey but you just have to process it one step at a time.
Do you know any way of identifying the data? Is the data prefixed with anything? Is the header to the file in a standard format? Is there a set pattern that the data forms? Checksum? Anything? After we have this we can help you impliment it into code.
Well the data loaded by fin.open() is going to be any .txt file greater than 4kb. The goal is to then split it into a set of files, each 4kb in size, with the same text as in the original text file. That's why I don't want to use getline, because it requires a specific type of character that may not be in the text file.
It is to be used as a way of getting large files of text onto my iPod, so i can read books off my iPod from gutenburg.org. So to say that the data follows a set pattern would not be true as the books can follow any pattern.
I hope that answered your questions... if not let me know and i'll do my best
Pfft, you're doing too much work my friend. 4kb of data in a .txt file will always be X number of characters. Find out what X is, round back to the last puctuation mark and parse away.
iPod huh? I remember looking at the Mac API once, I don't believe I have ever experinanced so much rage from simply reading a document at any other point in my life. It was like a dyslexic monkey skimmed Charles Babbages footnotes about the first computer then tried to build one from memory making software commands up on the fly. End of rant although I will point out the lack of a Mac section in any major C++ site ;) something to think about.
4096 characters, already figured out. I'm not actually using the MAC API or anything, I'm building this as a windows console app.
I don't quite understand how to do what you're saying though that is exactly what i want to do. Which is why i'm here. Care to show me an example of how you would do it?
int main()
{
//Code to read 4096 characters from a file to a string named FData
int i = FData.rfind('.', 4096); //Finds the LAST period in the FData string 4096 characters long
std::string MyData = FData.substr(0, i);
/*Populates a new string with everything from the first character in FData to the last period found.*/
//Output to newfile.
for(x = 0;x<number;x++,y++)
{
//Writing the file names
ostringstream child_file; //allows for dynamic manipulation of strings
string in_text(File_name_opened.c_str()),final_text;
int startp,endp,p_endp,last_char,p_last_char;
startp = 4096 * x;endp = 4096 * y;p_endp = 4096 * (y-1);
child_file << std_name << " Pt " << y << ".txt";
child_name = child_file.str();
fout.open(child_name.c_str());
if(fexists(child_name.c_str()) == false)
{
cout << "\nIn file " << child_name.c_str() << "\n";
cout << "Error: file unable to be made correctly\n";
pause();
return 1;
}
//Writing the file content
fin.open(File_name_opened.c_str());
//endpoint of previous iteration
p_last_char = in_text.rfind('.',p_endp);
if(p_last_char < 0){p_last_char = 0;}
//endpoint of this iteration
last_char = in_text.rfind('.',endp);
//what should go in the file
final_text = in_text.substr(p_last_char,last_char);
fout << final_text;
cout << "last character = "<< last_char << "\nFinal text = " << final_text << "\n";
fin.close();
fout.close();
}
I have a feeling i'm very very close to my goal, but I am unable to initialize in_text to the data that is in the opened text file, causing multiple lines to return -1.
so hopefully my last question on this project, how would I initialize the in_text to make it a string of the data inside File_name_opened.c_str()?
If you want to do it yourself, you need to treat the file as binary. The algorithm's something like:
1 2 3 4 5 6 7 8 9 10
declare 4k buffer
declare count := 0
open input file
while !input.eof
read buffer (last chuck will be less than 4k)
output file name = :count
open output file
write buffer to output file
increment count
end while
@ kbw: I thought about something like that at first to, but then I imagined how obnoxious it would be to have a text file end in the middle of a sentence which is where my suggestion about parsing on the punctuation came from. Do you think it adds too much work to the scope of the project?