Obviously I'm doing something wrong, I just don't know what. Here's my code:
When I print out fileTxt I get nothing. Any help is much appreciated.Code:char* fileTxt;
std::ifstream aFile;
aFile.open("a.txt");
aFile >> fileTxt;
a.close();
Printable View
Obviously I'm doing something wrong, I just don't know what. Here's my code:
When I print out fileTxt I get nothing. Any help is much appreciated.Code:char* fileTxt;
std::ifstream aFile;
aFile.open("a.txt");
aFile >> fileTxt;
a.close();
Also, if it helps I am running linux.
Annnnnnnd also that the last line I mistyped... it should be aFile.close();
Your char* is uninitialised and not pointing at a chunk of memory you can write to. You can either use new[] or malloc() to dynamically allocate some memory, or you can use an array, or the best option, swop the char* for a std::string
so
std::string fileTxt;
or
char fileTxt[256];
or
char* fileTxt = new char[256];
The string option is best as the memory managment is done for you by the string class. I used 256 as an example size, but im sure you get the idea. Remember malloc must be matched with a free(), new[] matched with a delete[]. The array is made on the stack so no special cleanup needed for that.