My friend and I are developing a program in C, and I'm trying to implement support for multiple languages. We're trying to create a text RPG, like Zork, and we're trying to make it customizable by loading in data from external plain-text files. As an example, the file containing data for the first room in English is located at res/en/room/001.txt. I've tried the following code, but it does not work and exits with status 255.

Code:

#include <stdio.h>
#include <stdlib.h>

char* roomFileName;

void setLang(char* lang)
{
    roomFileName[4] = lang[0];
    roomFileName[5] = lang[1];
}

void loadRoom(int roomNumber)
{
    char roomString[5];
    sprintf(roomString, "%03d", roomNumber);
    roomFileName[12] = roomString[0];
    roomFileName[13] = roomString[1];
    roomFileName[14] = roomString[2];

    FILE *roomFile;
    roomFile = fopen(roomFileName, "r");

    // Handle data

    fclose(roomFile);
}

int main()
{
    setLang("en");
    roomFileName = "res/xx/room/yyy.txt";
    loadRoom(1);

    // This is just to let me see if anything actually worked if the program exits properly
    printf("%s",roomFileName);
    return 0;
}

Can anybody help me see what might be the issue with this?
You're really not supposed to modify string constants (they're called constants for a reason). It might be generally better if you define a char array to hold the filename and do something like sprintf(filename, "res/%s/room/%03d.txt", lang, roomNumber);
Indeed, modifying a string constant like that is shady at best and a segfault at worst. Calc84's suggestion is pretty much the way you should do that. You can either use a statically-reserved string like char* roomFileName[255], or malloc() space for the string.
calc84maniac wrote:
You're really not supposed to modify string constants (they're called constants for a reason).
They're only constant if declared const, which these aren't. It's fine.

Oh, it's a pointer rather than a buffer. Yeah, that's bad. That should produce a few warnings when compiled.

calc84maniac wrote:
It might be generally better if you define a char array to hold the filename and do something like sprintf(filename, "res/%s/room/%03d.txt", lang, roomNumber);
Agreed, this is probably the cleanest way to do it.
Tari wrote:
calc84maniac wrote:
You're really not supposed to modify string constants (they're called constants for a reason).
They're only constant if declared const, which these aren't. It's fine.
Was I incorrect to be under the impression that some compilers might put string constants on memory pages marked read-only, no write/execute?
Edited after I noticed it's a pointer. He's discarding the implicit const qualifier from the string literal, so that should emit a warning (and it may do the wrong thing, depending on the platform).

Certainly such a thing on Prizm would be an attempted write to Flash, whatever the consequences of such an action.
Thanks, guys. I've been working with C++ a bit recently and have gotten used to using the string class, but I've been wanting to keep this in C to make sure I can grasp the concepts. I guess it's pretty clear I didn't make enough of an effort to understand arrays and pointers. Rolling Eyes
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement