Hey, I'm having a problem with my RPG. I'm trying to get a player to move from one room to another. All the coordinates (and other properties) of all the objects inside a given room are contained inside a text file, and this includes the properties of an exit to a different room. The exit has a position, sprite, and most importantly, a char* that holds the name of the script to look at to "build" the next room (obviously it isn't a char* inside the text file, my game just interprets it and stores it as a char*, which is a member of an Exit class). However, when it comes time to load the next room, the member that held the new room's filename is corrupt (not sure if that's the correct word - it basically no longer has the right data). I think there's an issue with how I store the filename.

else if(strcmp(buffer,"#exitNextRoomScript") == 0)
				{
					char next_room_script[MAX_PATH];
					fscanf(file,"%s",next_room_script);
					exit->SetNextRoomScript(next_room_script);
				}

This is the code the reads the string from the text file and stores in into an instance of the Exit class by calling Exit:: SetNextRoomScript(), defined here:

void Exit::SetNextRoomScript(char* filename)
{
	m_nextRoomScript = filename;
}

m_nextRoomScript is declared: char* m_nextRoomScript; , as a private member of the Exit class.

It is retrived with the Exit::GetNextRoomScript() method, defined as:

char* Exit:: GetNextRoomScript()
{
	return m_nextRoomScript;
}

Is there something wrong with the way I am letting the compiler convert a char array to a pointer to a char, or are they any other issues with scope I'm overlooking? thanks. :)

Why are you using the C way to read a file and store it's contents? Why not use std::strings, that would solve all your problems!

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

[.....]

string str_read;
ifstream fin("file.txt");
if (fin.is_open())
	cin >> str_read;
else cout << "couldn't open file";

Ehh... I just don't really like std::strings for some reason... I also don't like the idea of using << >> operators to for I/O, because shifting left and right have nothing to do with I/O.

But if that will solve my problem I guess I'd give it a go, but why exactly would it solve my problem?

because shifting left and right have nothing to do with I/O.

Ah, you have a 'C' mind, not a C++ ;)

But if that will solve my problem I guess I'd give it a go, but why exactly would it solve my problem?

Because it makes life easier. You won't have to throw pointers around to each function, but you could just use the entire string. Also thing like strcmp() and strcpy() can be replaced by the '==' and '=' operator.

std::string str = "abc";
if (str == "abc") str = "blablabla";

Ah, you have a 'C' mind, not a C++ ;)

What's the C++ mindset concerning overloading operators? That you shouldn't worry about the operator originally does with primitive data types? That it might as well be a new symbol, but C++ doesn't let you invent new operators?


Ehh... I guess I'll learn to use strings and the << >> operators for file I/O.

The buffer next_room_script[] lives in the scope of the containing if() block. Your problem will go away if you change the m_nextRoomScript to be a std::string because the string will copy and hold the data you have read from the file. The pointer version just points to the temporary buffer and gets garbled.

The buffer next_room_script[] lives in the scope of the containing if() block. Your problem will go away if you change the m_nextRoomScript to be a std::string because the string will copy and hold the data you have read from the file. The pointer version just points to the temporary buffer and gets garbled.

Right... I thought it might be something like that....

So basically when I pass a char array to a function that takes a pointer, I'm taking the address of the array, which goes out of scope at the end of the if block. If I were to use a string, I would be copying the actual data to a new memory location, not just copying the memory location, right? Now, if I did that, would I need to change the data type of m_nextRoomScript to string, or would there be a way to still keep that as a char*?

Right... I thought it might be something like that....

So basically when I pass a char array to a function that takes a pointer, I'm taking the address of the array, which goes out of scope at the end of the if block. If I were to use a string, I would be copying the actual data to a new memory location, not just copying the memory location, right? Now, if I did that, would I need to change the data type of m_nextRoomScript to string, or would there be a way to still keep that as a char*?

I think you've understood it now. I'm guessing that you desire to have the char* in order for to write the data to a file or compare it (using strcmp())... you can very well use a std::string and wherever you need a const char * to the data held by the std::string, you can use the std::string' c_str() function which returns a const C-style string.

Trying to make this clearer ...

void function()
{
    char regular_arr[20]= "abc";
    std::string std_string = regular_arr; // copies the 'abc' into the string

    if(0 == strcmp(regular_arr, std_string.c_str()))
    {
        // the stríngs are identical, do something ...
    }

    FILE * fp = fopen("a.txt", "w");

    if(fp)
    {
        // write the string to file
        fputs(std_string.c_str(), fp);
    }
}
This article has been dead for over six months. Start a new discussion instead.