Thread: Trying to save a simple boolean into a data file, and load it

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Trying to save an array of variables to load at a later time, not working correctly..

    Code:
    bool savevars(int vars[28][14])
    {
    	using std::ofstream;
    	int loop;
    	int loop1;
    	char *temp = new char;
    	ofstream filestr;
    	filestr.open ("data.dat");
    	if (filestr.is_open())
    	{
    		for (loop=1;loop<=28;loop++)
    		{
    			for (loop1=1;loop1<=14;loop1++)
    			{
    				wsprintf(temp,"%d",vars[loop][loop1]);
    				filestr <<temp<<'\n';
    			}
    		}
    	}
    	else
    	{
    		return false;
    	}
    	return true;
    	filestr.close();
    
    }
    
    bool loadvars()
    {
    	using std::ifstream;
    	int j, k;
    	extern int sectionone[28][14];
    	ifstream filesstr;
    	filesstr.open("data.dat", ios::in);
    	if (filesstr.is_open())
    	{
    		for (j=1;j<=28;j++)
    		{
    			for (k=1;k<=14;k++)
    			{
    				filesstr >> sectionone[j][k];
    			}
    		}
    	}
    	else
    	{
    		return false;
    	}
    	return true;
    	filesstr.close();
    
    }
    its saving correctly, the file changes in the correct ways every time it is saved, but the loading isnt working... Or is it the other way around?

    Help??
    Last edited by Shamino; 09-09-2005 at 09:34 AM.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    char *temp = new char;
    This is a single char, you want an array:
    Code:
    char *temp = new char[SOME_SUITABLE_SIZE];
    for (loop=1;loop<=28;loop++)
    for (loop1=1;loop1<=14;loop1++)
    This is bad. Arrays in C++ are zero based, so index when loop is 28 or loop1 is 14, you're accessing the array out of bounds. It should be:
    Code:
    for (loop=0;loop<28;loop++)
    for (loop1=0;loop1<14;loop1++)
    Repeat ad infinitum until your loops are all correct.
    wsprintf(temp,"%d",vars[loop][loop1]);
    You're using a wide stream, but a narrow buffer. That can cause problems. Either stick with sprintf, or make temp an array of wchar_t.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    return true;
    filestr.close();
    The fstream's destructor will close the file on its own without you needing to explicitly call the close member function. In any case, if you do want to include an explicit call, it must go before the return statement and not after.

    Code:
    wsprintf(temp,"%d",vars[loop][loop1]);
    filestr <<temp<<'\n';
    Why not just:
    Code:
    filestr << vars[loop][loop1] << '\n';
    "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

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Here's my new code, works perfectly good

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream.h>
    #include <windows.h>
    #include "function.h"
    
    bool savevars(int vars[15][29])
    {
    	using std::ofstream;
    
    	char *temp = new char;
    	ofstream filestr;
    	filestr.open ("data.HC");
    	if (filestr.is_open())
    	{
    		for(j=1;j<=28;j++)
    		{
    			for(k=1;k<=14;k++)
    			{
    				wsprintf(temp,"%d",vars[k][j]);
    				filestr << temp << '\n';
    			}
    		}
    	}
    	else
    	{
    		return false;
    	}
    	return true;
    	filestr.close();
    
    }
    
    bool loadvars()
    {
    	using std::ifstream;
    
    
    	extern int sectionone[15][29];
    	ifstream filesstr;
    	filesstr.open("data.HC");
    	if (filesstr.is_open())
    	{
    		for(j=1;j<=28;j++)
    		{
    			for(k=1;k<=14;k++)
    			{
    				filesstr >> sectionone[k][j];
    			}
    		}
    	}
    	else
    	{
    		return false;
    	}
    	return true;
    	filesstr.close();
    
    }
    /////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////
    //Save/Load tickets and Total amount of money////////////////////////////////////
    
    bool SaveTic(int numtic)
    {
    	using std::ofstream;
    	ofstream savefile;
    	savefile.open("ticket.HC");
    
    	if(savefile.is_open())
    	{
    		savefile << numtic;
    	}
    	else
    		return false;
    
    	return true;
    
    	savefile.close();
    }
    
    bool Loadtic()
    {
    	using std::ifstream;
    	ifstream Loadfile;
    	Loadfile.open("ticket.HC");
    
    	if(Loadfile.is_open())
    		Loadfile >> totaltic;
    	else
    		return false;
    	return true;
    
    	Loadfile.close();
    }
    
    
    
    
    
    bool SaveAmount(int amount)
    {
    	using std::ofstream;
    	ofstream Savefile;
    	Savefile.open("money.HC");
    
    	if(Savefile.is_open())
    		Savefile << amount;
    	else
    		return false;
    	return true;
    	Savefile.close();
    }
    
    
    bool LoadAmount()
    {
    	using std::ifstream;
    	ifstream Loadfile;
    	Loadfile.open("money.HC");
    
    	if(Loadfile.is_open())
    		Loadfile >> totalprice;
    	else
    		return false;
    	return true;
    	Loadfile.close();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Save and Load
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 05-13-2009, 12:49 AM
  2. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  3. Replies: 11
    Last Post: 04-17-2008, 02:29 AM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM