1

I know the title is a little confusing, but I don't know how to explain it.

void save (POINT pt)
    {
    ofstream save;
    save.open("coords.txt", ios::trunc);

    if (save.is_open())
    {
        save << pt.x << endl;
        save << pt.y;

        save.close();

        system("CLS");

        cout << "Save successful\n\n\n\n";

        system("PAUSE");
    }

    else
    {
        system("CLS");

        cout << "Error: Could not save\n\n\n\n";

        system("PAUSE");
    }
    }

    int load ( )
    {
        ifstream load;
        load.open("coords.txt", ifstream::in);

        if (load.is_open())
        {

        }
    }

I want to read the POINT from the save function inside of the load function. I tried

 if (load.is_open())
    {
        load >> pt.x;
        load >> pt.y;
    }

but pt.x and pt.y are undefined. I'm not very good at this, but I 'm trying to understand it.

Thanks in advance!

KenD
  • 5,280
  • 7
  • 48
  • 85
x0x Rain x0x
  • 11
  • 1
  • 4
  • I'm unsure of what you're asking – tay10r Jun 01 '13 at 07:51
  • So, what I'm doing is an auto clicker. I have a set coordinate in the program already but I added a calibration option where the user could input their own coordinates to fit a different screen resolution. I want to save the coordinate into a file and when they reopen the program I want to be able to read the file and use the coordinates as a default. – x0x Rain x0x Jun 01 '13 at 07:55
  • it seems like your problem is that you haven't declared a POINT structure in the load function. – tay10r Jun 01 '13 at 08:10
  • But when I declare a POINT structure in the load function it fails because it hasn't been initialized. – x0x Rain x0x Jun 01 '13 at 08:16
  • What do you mean it fails? Have you looked at the answer I provided? – tay10r Jun 01 '13 at 08:17
  • Nevermind, I apologize. When I initialized the new POINT structure it gave me back the coordinates I saved in the save function. I guess I just didn't understand why it worked that way. – x0x Rain x0x Jun 01 '13 at 08:21

5 Answers5

0

Using plain (standard) iostream objects for serialization/deserialization can lead to a lot of pain.

I suggest you to use some higher-level serialization library like boost::archive::binary_oarchive and boost::archive::binary_iarchive (link to the doc).

It is quite easy to use, avoir you a lot of issues, and gives you the opportunity to discover a very useful C++ library.

See this post for more information.

Community
  • 1
  • 1
Matthieu Rouget
  • 3,289
  • 18
  • 23
0

Try this :

if (load.is_open())
{
while(load.good())    
{
   getline(load,point);
   pt.x=line;
   getline(load,point);
   pt.y=line;
    }
else{
 file open failed.
}

assuming pt.x and pt.y as string. if they are integer then you need to convert the point to integer.

Arpit
  • 12,767
  • 3
  • 27
  • 40
0

Don't you just need to declare pt? Or is it a member?

int load ( )
    {
        POINT pt;
        ifstream load;
        load.open("coords.txt", ifstream::in);

        if (load.is_open())
        {
            load >> pt.x;
            load >> pt.y;
        }
    }
Zaphod
  • 1,927
  • 11
  • 13
0

As the previous poster suggestes, plain iostream might not be the best choice...

albeit in this case, your problem seems to be lack of definition of POINT pt in the scope of load() functions.

Perhaps create a singleton responsible for settings?

class MySettings
{
private:
    POINT m_pt;
public:
    static MySettings* instance()
    {
         static MySettings settings;
         return &settings;
    }

    int save(POINT pt) {...}
    POINT load() {...}
}
hauron
  • 4,550
  • 5
  • 35
  • 52
0

It seems the answer is quite obvious, you need to add a POINT argument to load:

/* The & symbol gives you back the points when the function has returned */
/* Alternatively, more conventionally, you may use a pointer. */

int load (POINT & pt){
    ifstream load;

    load.open("coords.txt", ifstream::in);

    if (!load.is_open()){
        cerr << "File couldn't be opened" << endl;
        return -1;
    }

    load >> pt.x;
    load >> pt.y;

    load.close();

    return 0;
}

After this function has returned, the pt argument contains the points you're looking for. You would call it like this:

POINT points;
load (points);
tay10r
  • 4,234
  • 2
  • 24
  • 44