Hi, I'm a complete noob to C++. I am currently just making text games where you input a string and depending what that string is and what you have in your "inventory", the program will cout something else.
I would like to know how i would be able to save a value, like the intiger "gun", with a value of lets say 1 to say that you have it in your possession.
How would i save this value to a different folder that the program can later read? Like when the user types in "save", it saves all current values to a specific folder, and when the user gets back on, it checks to see if that folder exists, and then what the values are.
I'm in no great hurry, and would kind of like it if the basic stuff could be explained to me. I want to make a really long text game, but that requires saving.
(oops, im rambling...) Thanks!

Do you know how to write to a file? That's pretty much all you need to know.

that didnt help at all.
i am totally new to C++.
i know how to create a txt file and add some words, and thats it.
how do i make it so that it can get the value as a value?
and how can i make it so that it saves the value as a value?

Go Try http://www.cplusplus.com/doc/tutorial/files/, it has pretty much everything you need to know about basic writing to and from files.

iv gone there actually, and when i use the code to try and get the words from the file, it just says "file cannot be opened".
and even then, i dont know how to make it so that it would get whatever is there to be placed as a value.

ok, im off for the night.
i really would like some help with this, eventually i just want to make a text game thats HUGE.
like the size of a bored mans "D&D" game.
but i need to be able to save to do that.
byby!

Edited 6 Years Ago by 006ruler: acuracy.

Can you paste the code that you used to get the error : "file cannot be opened" ?
And
I don't get what you mean by

i dont know how to make it so that it would get whatever is there to be placed as a value.

We aren't psychic. You need to show us what you tried so we can help you fix it. Did you read the forum rules? Did you read any of the sticky posts at the top of the forum?

when i ran this program, it said that it was unable to open the file. The "else" part of the program. What did i do wrong, and how do i fix it? I already know that the file exists, i used a different program to make it.
And if i do get it to successfully open up the "example.txt" file, how do i assign numbers that may be in it to a value?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string line;
    ifstream myfile ("example.txt");
    if (myfile.is_open())
    {
        while (! myfile.eof())
        {
            getline (myfile, line);
            cout << line << endl;
        }
        myfile.close();
    }
    else cout << "Unable to open file";

    return 0;
}

Edited 6 Years Ago by WaltP: Learn to use the PREVIEW button

>when i ran this program, it said that it was unable to open the file.
The file probably isn't where ifstream is looking. Try calling perror to see if it gives you more information.

>when i ran this program, it said that it was unable to open the file.
The file probably isn't where ifstream is looking. Try calling perror to see if it gives you more information.

im really nooby.
what is perror?
and how do i use it?

and is ur name from the manga "love hina"?

and how do i specify where ifstream should be looking?

>im really nooby.
>what is perror?
>and how do i use it?

Clicky.

>and is ur name from the manga "love hina"?
No.

>and how do i specify where ifstream should be looking?
Well, you could give an absolute path rather than a relative path for the file name. It's probably looking in the current working directory, and your file isn't there.

YAY!
ok, so the the program now succesfully reads what is in the text file and places it on the screen. How would i make it so that when a number is found, it is placed as a value?

ifstream PRODUCT ("PRODUCT.txt");
     if (PRODUCT.is_open()){

                          
                          do{
                          
                          PRODUCT >> PRICE; // put whatever global variable you require
  
             
}while (!PRODUCT.eof() );

Edited 6 Years Ago by cyberguy007: n/a

In your .txt file, if you want to read in more than one value into your program, make sure you put a space between each number, as file >> will only read in one value until a space, if you want to read in a whole like use getline(file, your variable.....)

Sorry I accidentally posted and it won't let me delete my post.

Edited 6 Years Ago by cpeister: n/a

This article has been dead for over six months. Start a new discussion instead.