Problem loading in files with fstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Problem loading in files with fstream

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Question 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.

  2. #2
    Join Date
    Dec 2009
    Posts
    3

    Re: Problem loading in files with fstream

    Also, if it helps I am running linux.

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    Re: Problem loading in files with fstream

    Annnnnnnd also that the last line I mistyped... it should be aFile.close();

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center