more fstream problems

This is a discussion on more fstream problems within the C++ Programming forums, part of the General Programming Boards category; There's been a lot of these up here lately, but... basically, this doesn't work Code: void genPlayer(hck::plyr player[], int playerid){ ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    more fstream problems

    There's been a lot of these up here lately, but...

    basically, this doesn't work
    Code:
    void genPlayer(hck::plyr player[], int playerid){
         char* filename;
         strcpy(filename,player[playerid].alias.c_str());
         strcat(filename,".hack");
         cout << filename << endl; /*debugging line to see if the filename is right, not needed*/
         cin.ignore(); /*and a pause so i can look at it*/
         ofstream player_file;
         player_file.open(filename);
         player_file << player[playerid].alias << " ";
         player_file.close();
         return;
         }
    but if i take the code out of the function and put it in main(), it works fine...

    Can someone explain?(and hopefully give a solution)

    edit: Also, when i use it like this, it doesn't cout filename like I tell it to, and it disregards my cin.ignore()
    Last edited by howzer; 03-15-2006 at 05:27 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    char* filename;
    strcpy(filename,player[playerid].alias.c_str());
    strcat(filename,".hack");
    Tell me, where does filename point to? You can't strcpy into a location pointed to by an uninitialized pointer. You'll end up overwriting god knows what. Better to stick with a string object.

    Code:
    void genPlayer(hck::plyr player[], int playerid)
    {
        string filename(player[playerid].alias);
        filename += ".hack";
        cout << filename << endl; /*debugging line to see if the filename is right, not needed*/
        cin.get(); /*and a pause so i can look at it*/
        ofstream player_file(filename.c_str());
        player_file << player[playerid].alias << " ";
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    don't worry about that char* filename, it was an attempt by me to fix the problem, but it still didn't work right. but thanks to you, problem solved!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd fstream problems...
    By Jaken Veina in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 05:44 PM
  2. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  3. unusual fstream problems
    By georgefoxjunior in forum C++ Programming
    Replies: 11
    Last Post: 09-09-2004, 09:21 AM
  4. fstream problems
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 10:44 AM
  5. stl fstream error (yes... file problems again)
    By ygfperson in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2002, 11:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21