fstreams dont work!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

fstreams dont work!

Post by acerookie1 »

i pass a string to both of these functions and they dont save nor load the files i want.

the loading and saving:

Code: Select all

void Level::Save_Map(string save, int H, int W)
{
    ofstream Save;
    cout<<"Saving to: "<< save <<endl;
    Save.open( save.c_str() );

    Save<<"H "<< H <<" W "<< W <<"\n\n";
    if(!Save.bad())
    {
        for(int a = 0; a < z; a++)
        {
            for(int b = 0; b < x; b++)
            {
                for(int c = 0; c < y; c++)
                {
                    Save<<"Layer "<< a << " Tile "<< c;
                    Save<<" X "<< tile[a][b][c].x <<" Y " << tile[a][b][c].y;
                    Save<<" Strip_x "<< tile[a][b][c].s_x;
                    Save<<" Strip_y "<<tile[a][b][c].s_y;
                    Save<<" Hieght "<< H;
                    Save<<" Width "<< W;
                    Save<<" END \n\n";
                }
                Save<<"\n";
            }
            Save<<"\n \n \n";
        }
    }
    else
    {
        cout<<"File failed creation"<<endl;
    }
    Save.close();

}

void Level::Load_Map(string load)
{
    ifstream Load;
    Load.open( load.c_str() );
    string temp;
    if(Load.is_open())
    {
        cout<<"Found file"<<endl;
        while(Load>>temp)
        {
            if(temp == "Layer")
            {
                Load>>layer;
            }
            if(temp == "Tile")
            {
                Load>>tile_count;
            }
            if(temp == "X")
            {
                Load>>x_coord;
            }
            if(temp == "Y")
            {
                Load>>y_coord;
            }
            if(temp == "Strip_x")
            {
                Load>>strip_x;
            }
            if(temp == "Strip_y")
            {
                Load>>strip_y;
            }
            if(temp == "H")
            {
                Load>>h;
            }
            if(temp == "W")
            {
                Load>>w;
            }
            if(temp == "END")
            {
                tile[layer][x_coord / w][y_coord / h].x = x_coord;
                tile[layer][x_coord / w][y_coord / h].y = y_coord;
                tile[layer][x_coord / w][y_coord / h].s_x = strip_x;
                tile[layer][x_coord / w][y_coord / h].s_y = strip_y;
                tile[layer][x_coord / w][y_coord / h].h = h;
                tile[layer][x_coord / w][y_coord / h].w = w;
            }
        }
    }
    else
    {
        cout<<"Could not open file:"<< load <<endl;
    }
    Load.close();
}
here's the code that sends the string:

Code: Select all

level.Save_Map(console.entered,32,32);
i got the console workin fine its just when i try to send it to the level it will get what i sent but it wont save to the name of it.
which proves that its gettin it but not loadin or savin.
ex: in Level::Save():

Code: Select all

    cout<<"Saving to: "<< save <<endl;
thanx in advance. :bow: if you want console code i can post too.
Image
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: fstreams dont work!

Post by xiphirx »

Try this modified code, I don't think it will do anything, but its worth a shot. Posting your console code will help alot too.

Code: Select all

void Level::Save_Map(string save, int H, int W)
{
    ofstream Save(save.c_str());
    cout<<"Saving to: "<< save <<endl;

    if(Save)
    {
	Save<<"H "<< H <<" W "<< W <<"\n\n";
        for(int a = 0; a < z; a++)
        {
            for(int b = 0; b < x; b++)
            {
                for(int c = 0; c < y; c++)
                {
                    Save<<"Layer "<< a << " Tile "<< c;
                    Save<<" X "<< tile[a][b][c].x <<" Y " << tile[a][b][c].y;
                    Save<<" Strip_x "<< tile[a][b][c].s_x;
                    Save<<" Strip_y "<<tile[a][b][c].s_y;
                    Save<<" Hieght "<< H;
                    Save<<" Width "<< W;
                    Save<<" END \n\n";
                }
                Save<<"\n";
            }
            Save<<"\n \n \n";
        }
    }
    else
    {
        cout<<"File failed creation"<<endl;
    }
    Save.close();

}

void Level::Load_Map(string load)
{
    ifstream Load(load.c_str());
    string temp;
    if(Load)
    {
        cout<<"Found file"<<endl;
        while(!Load.eof())
        {
		Load >> temp;
            if(temp == "Layer")
            {
                Load>>layer;
            }
            if(temp == "Tile")
            {
                Load>>tile_count;
            }
            if(temp == "X")
            {
                Load>>x_coord;
            }
            if(temp == "Y")
            {
                Load>>y_coord;
            }
            if(temp == "Strip_x")
            {
                Load>>strip_x;
            }
            if(temp == "Strip_y")
            {
                Load>>strip_y;
            }
            if(temp == "H")
            {
                Load>>h;
            }
            if(temp == "W")
            {
                Load>>w;
            }
            if(temp == "END")
            {
                tile[layer][x_coord / w][y_coord / h].x = x_coord;
                tile[layer][x_coord / w][y_coord / h].y = y_coord;
                tile[layer][x_coord / w][y_coord / h].s_x = strip_x;
                tile[layer][x_coord / w][y_coord / h].s_y = strip_y;
                tile[layer][x_coord / w][y_coord / h].h = h;
                tile[layer][x_coord / w][y_coord / h].w = w;
            }
        }
    }
    else
    {
        cout<<"Could not open file:"<< load <<endl;
    }
    Load.close();
}
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

Re: fstreams dont work!

Post by acerookie1 »

figured it out. well i didnt fix it yet. its what ever sfml converts text to plus its in unicode format. but the level class gets it but nothing related to it can handle the string. ex: i cant use the inputed text to save. but when i use cin for input it works fine saving. idk whats up.
Image
Post Reply