-
December 8th, 2009, 10:00 PM
#1
Problem loading in files with fstream
Obviously I'm doing something wrong, I just don't know what. Here's my code:
Code:
char* fileTxt;
std::ifstream aFile;
aFile.open("a.txt");
aFile >> fileTxt;
a.close();
When I print out fileTxt I get nothing. Any help is much appreciated.
-
December 8th, 2009, 10:02 PM
#2
Re: Problem loading in files with fstream
Also, if it helps I am running linux.
-
December 8th, 2009, 10:26 PM
#3
Re: Problem loading in files with fstream
Annnnnnnd also that the last line I mistyped... it should be aFile.close();
-
December 8th, 2009, 10:41 PM
#4
Re: Problem loading in files with fstream
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.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|