Thread: C++ Code not adding text to text file

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    8

    C++ Code not adding text to text file

    I have a code but the problem is that it isn't putting the desired text to a text file. I have to have it in a function, can't have it in main. I do include stream, upstream, string, etc.
    Code:
    void gamesave(string gold, string health) {
        ofstream savefile ("data.txt", ios::out | ios::app | ios::binary);
        if (savefile.is_open()) {
            savefile << gold << "\n" << health << "\n";
        } else {
    cout << RED << "Warning! Your file did not save" << endl;
        }
    }
    Why is it not adding the desired text to data.txt?

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    Works for me. Have you made sure that the passed strings contain any data?

    In future posts, place your code inside [code][/code] tags.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    I did put it in those tags. It must not show up.
    Anyways, the strings are numbers ex: "1", "109", which get the data from integers. So I convert the int value into a string, so it always has a value 0+
    Last edited by Vanillla; 08-17-2017 at 04:21 PM.

  4. #4
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    Whoops, sorry about that, did the wrong code, the above one does work, I think I might have too many variables but I am not sure

    void gamesave(string sslimychestplate, string sslimyhelmet, string soozinglegplates, string soozingboots, string spiclass, string slevel, string sxp, string sxpb, string sxpl, string sstre, string sinte, string sdext, string shpotion, string sgold, string sskill, string scobalt, string scbomb) {
    ofstream savefile ("data.txt", ios::out | ios::app | ios::binary);
    if (savefile.is_open()) {
    savefile << "\n";
    savefile << sgold << "\n" << sstre << "\n" << sinte << "\n" << sdext << "\n" << sskill << "\n" << sxp << "\n" << sxpl << "\n" << sxpb << "\n" << slevel << "\n" << scobalt << "\n" << scbomb << "\n";
    savefile << shpotion << "\n";
    savefile << spiclass << "\n" << sslimyhelmet << "\n" << sslimychestplate << "\n" << soozinglegplates << "\n" << soozingboots << "\n";
    } else {
    cout << RED << "Warning! Your file did not save" << endl;
    }
    }
    Last edited by Vanillla; 08-17-2017 at 04:34 PM.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Edit your post and paste your code as plain text. Your formatting is screwing things up.
    In what sense is it "not adding the desired text"?
    Is it adding anything at all?

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    It is not adding anything at all, so when I open the document data.txt, it is blank

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    I did put it in those tags. It must not show up.
    I think when you copy your code, your editor copies all its formatting (font, syntax highlighting) with it, instead of just the plain text. My editors don't do this, but one solution I've used in the past is to first copy the code into a simple text editor like notepad, which strips the formatting, and then copy it again from there.

    Your second example doesn't present a tangible difference to your first, just more variables. So I suspect your problem lies elsewhere. You should start small and build up until you hit the error, that's a good way to debug this.

    Instead of passing so many parameters, considering if a compound type like a struct makes sense for this data, e.g.:
    Code:
    struct PlayerState {
        std::string level;
        std::string xp;
        std::string health;
        …
    }
    
    void gamesave(const PlayerState& playerstate) {
        savefile << playerstate.level << "\n"
                 << playerstate.xp << "\n"
                 << playerstate.health;
    }

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    With the struct is it possible for me to input variables from my main.cpp?

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Why are you opening the file in "binary" mode when you are obviously writing text to it?
    And you don't need ios::out, either, since ofstream is an output stream. Try:
    Code:
        ofstream savefile ("data.txt", ios::app);
    Still, you should have seen something in the file. Try deleting the file and running the program. That way, if it's creating the file in another directory you will notice that it's not where you think it should be.

  10. #10
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    With the struct is it possible for me to input variables from my main.cpp?
    Sure, you can just assign to it:
    Code:
    PlayerState player;
    player.level = "3";
    player.xp = "840";
    player.health = "95";
    
    gamesave(player);
    But let's focus on your problem and not go offtopic, you can stick to your many parameters if you haven't learned about structs yet.

    Have your tried writing to a file in main() yet? You should start there. And make sure you're actually looking at the right data.txt, and not some older path that's not being written to.

    p.s. It would be far more idiomatic to use numeric types like int or double to store your number values. Strings are mainly intended for text, e.g. names.
    Last edited by -Adrian; 08-17-2017 at 04:52 PM.

  11. #11
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    So, it is saving to a file, but not the file where the main.cpp is, so would that mean I would have to add the directory to the file?

  12. #12
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    If you just specify "data.txt", the file will be created in the same directory where your executable is.

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your function signature is insanity squared. Your character is in dire need of a struct. Buy one at the local struct merchant.

    And I despise the habit of putting 's' in front of all the variables to indicate that they are strings. It's up to you, but it's not normally done and just makes your variables sound stupid. (It reminds me of "selectron", "sneutrino", etc., the horrible names of the super-symmetric particles in physics.)

  14. #14
    Registered User
    Join Date
    Aug 2017
    Posts
    8
    The executable, data.txt, and the main.cpp, are all in the same file, but the data.txt file is still being created in Macintosh HD\Users\MyUser
    I only put 's' in front of all the variables is because I have gold, hp, and xp as integers.

  15. #15
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    Hmm, honestly no idea then. I've never encountered that behavior.

    I only put 's' in front of all the variables is because I have gold, hp, and xp as integers.
    Then you should keep them as integers, instead of converting to strings. You're doing math in your program, no? The stream classes like std:fstream are designed in a way that allows you to insert numeric types using the << operator all the same.

    I gotta go, maybe algorism is around a bit longer to sort this out with you. I suspect it's some mix up not directly caused within the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-23-2017, 04:20 AM
  2. Making a string from a text file after a specific text
    By GypsyV3nom in forum C Programming
    Replies: 4
    Last Post: 06-22-2016, 01:11 PM
  3. adding text from file to a linked list
    By gioullis in forum C Programming
    Replies: 2
    Last Post: 06-05-2012, 11:29 AM
  4. Replace wildcard text in file with specific text.
    By untz in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2010, 06:35 PM
  5. Replies: 4
    Last Post: 06-22-2002, 12:23 PM

Tags for this Thread