Actually, I haven't though about using an array of strings. Thanks for bringing that up. Your proposition isn't exactly what I'm looking for as it still prints out to the stdout (cout) in C++. I want whatever's read to be fed into a char array. But your response led me to the answer I was looking for, so pat yourself on the back :D.
Lo and behold, this is what I was looking for:
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
|
#include <iostream>
#include <fstream>
#include "TextFile.h"
TextFile *OpenFile (const char *name)
{
std::string line;
TextFile *file = new TextFile();
std::ifstream text (name);
if (text.is_open())
{
while (text.good())
{
getline(text,line);
file->content.append(line);
file->content.push_back('\n');
}
text.close();
}
else
{
std::cout << "Unable to open file" << std::endl << std::endl;
}
return file;
}
|
One thing to note before I quickly say what the above function does is that I
replaced the
char content[500]
in the TextFile class above (in my first post) with
std::string content
. This lead to things being much easier!
Anway, the OpenFile function takes an outside text file, parses it
line by line, and puts it into the content variable of a TextFile object (but you can reformat the code to fit any object you're dealing with)...So in short, this function transforms an external text file into a C++ object with its contents stored within it <3. Hooray!
Thanks for the epiphany lk2019 :D!
(SIDE NOTE: For all you file streamers out there using C++, std::string is the way to go for anything that is PURE text. Forget about using char[] or char * --- std::string was built for that stuff :] )