Thread: How to read multiple lines from a plain text file and store as a single variable.

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    9

    How to read multiple lines from a plain text file and store as a single variable.

    I've been working on a little dungeon crawling adventure just for fun. It only consists of 25 rooms at the moment, so it's very small. My little game works perfectly, but I'm currently trying to make a little improvement to the code.

    Here is what the game looks like to give you a better idea of what I'm doing:

    Code:
    You are standing in front of an archway that leads to the north. The
    inscription above the archway reads, "Your adventure begins."
    Exits: N
    > n
    
    
    You are standing at an intersection.
    Exits: N, E, S
    > e
    
    
    You are standing in a small cavern. You notice a brass key lying on
    the floor.
    Exits: W
    > get
    
    
    You pick up the brass key and put it in your satchel.
    
    
    You are standing in a small cavern. There is nothing in here.
    Exits: W
    > w
    
    
    You are standing at an intersection.
    Exits: N, E, S
    > n
    
    
    You are going around a bend.
    Exits: E, S
    > e
    
    
    You are standing in an east-west hallway.
    Exits: E, W
    > e
    
    
    You are standing in front of a closed door.
    Exits: W
    > open
    
    
    You are able to unlock the door with your brass key.
    
    
    You are standing in front of an open door.
    Exits: E, W
    >
    Right now I have all the room descriptions stored as char * variables in the main source code.

    Example:
    Code:
    char *ROOM14 =
    "You enter into a mist filled room. A layer of water vapor covers the\n\
    walls and ceiling. You feel heat radiating near the west wall.";
    
    
    char *ROOM15 =
    "The air in this room is dry and warm. The breeze you feel near the north\n\
    exit is cooler. The west wall is warm to the touch.";
    
    
    char *ROOM16 =
    "You are in a north-south cooridor. There is writing on the walls, but\n\
    it has become too faded to make out what it says.";
    
    
    char *ROOM17 =
    "This passageway seems familiar. Or does it? You are starting to\n\
    question your sense of direction.";
    I want to move these room descriptions into a plain text file. I know how to work with files, but I'm not sure how to read multiple lines of text from a file and store it as a single variable.

    My room data file looks like this at the moment:
    Code:
    room#: 0
    nExit: 1
    eExit: -1
    sExit: -1
    wExit: -1
    uExit: -1
    dExit: -1
    
    
    room#: 1
    nExit: 3
    eExit: 2
    sExit: 0
    wExit: -1
    uExit: -1
    dExit: -1
    
    
    room#: 2
    nExit: -1
    eExit: -1
    sExit: -1
    wExit: 1
    uExit: -1
    dExit: -1
    
    
    room#: 3
    nExit: -1
    eExit: 4
    sExit: 1
    wExit: -1
    uExit: -1
    dExit: -1
    This format works perfectly well and I'm able to parse the data with no problem. I want to add another line to this room data file for the room descriptions ... something like this:

    Code:
    room#: 14
    nExit: -1
    eExit: 11
    sExit: 13
    wExit: 22
    uExit: -1
    dExit: -1
    descr: "You enter into a mist filled room. A layer of water vapor covers the\n\ walls and ceiling. You feel heat radiating near the west wall."
    
    room#: 15
    nExit: 16
    eExit: 9
    sExit: -1
    wExit: 20
    uExit: -1
    dExit: -1
    descr: "The air in this room is dry and warm. The breeze you feel near the north\n\exit is cooler. The west wall is warm to the touch."

    However, I'm not sure how to parse out the "descr" line and store it as a single variable. So my question is, how can I move these room descriptions out of the source code file and into a plain text file while still being able to retain the room descriptions as a single variable?

    Thank you.

    EDIT:

    To add to this, here's the block of code I'm using to parse out the north, east, south, west, up, down directions.

    Code:
        // opening file for reading
        fp = fopen("rooms.txt" , "r");
        if (fp == NULL) {
            perror("Error opening rooms.txt file");
            return(-1);
        }
    
    
        // This loop reads through the rooms.txt file and sets the exit
        // points for each room.
        while(fgets(str, sizeof(str), fp) != NULL) {
            token = strtok(str, ":");
            if (strcmp(token, "room#") == 0) {
                token = strtok(NULL, "");
                roomNumber = atoi(token);
                for (directionCount = 0; directionCount < 6; directionCount++) {
                    fgets(str, sizeof(str), fp);
                    token = strtok(str, ":");
                    if (strcmp(token, "nExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].nExit = atoi(token);
                    }
                    if (strcmp(token, "eExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].eExit = atoi(token);
                    }
                    if (strcmp(token, "sExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].sExit = atoi(token);
                    }
                    if (strcmp(token, "wExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].wExit = atoi(token);
                    }
                    if (strcmp(token, "uExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].uExit = atoi(token);
                    }
                    if (strcmp(token, "dExit") == 0) {
                        token = strtok(NULL, "");
                        room[roomNumber].dExit = atoi(token);
                    }
                }
            }
        }
    
    
        // close file
        fclose(fp);
    And here is how I'm currently handling the room descriptions.

    Code:
        // Reset the roomNumber variable
        roomNumber = 0;
    
    
        // Setup the default room description for each room.
    
    
        // room 0
        strcpy(room[roomNumber].description, ROOM00);
        roomNumber++;
    
    
        // room 1
        strcpy(room[roomNumber].description, ROOM01);
        roomNumber++;
    
    
        // room 2
        strcpy(room[roomNumber].description, ROOM02);
        roomNumber++;
    
    
        // room 3
        strcpy(room[roomNumber].description, ROOM03);
        roomNumber++;
    
    
        // room 4
        strcpy(room[roomNumber].description, ROOM04);
        roomNumber++;
    If I can handle these room descriptions assignments inside of the loop, it will be much more elegant.
    Last edited by blixel; 03-07-2020 at 04:17 PM.

  2. #2
    Registered User
    Join Date
    Jul 2017
    Posts
    9
    I came up with a solution for this question. May not be the greatest, but it works.

    I created a new file I'm calling descriptions.txt, and I'm putting the room descriptions in there in this format:

    Code:
    ROOM007
    You are standing in a passageway at the bottom of a steep stairwell.
    }
    
    
    ROOM008
    You step onto a rickety bridge. Before you can react, the rotted wooden
    planks begin breaking out from underneath of you. You fall a long way and
    SMACK onto a hard canyon floor. In the final seconds of your life, you
    hear a swarm of rats gather around your broken body as they begin feasting
    on your flesh!
    }
    
    
    ROOM009
    You walk into a pitch black room. You'll need to find a light source
    so you can see your way around.
    }
    
    
    ROOM010
    You are at a bend in the path. There's an unpleasent stench in here.
    }
    
    
    ROOM011
    You enter a very dark room. As your eyes adjust, you can see an exit
    to the south. You hear the sound of falling water nearby.
    }
    Here's the new parser I wrote:

    Code:
        // Set each room description to "" by default.
        for (roomNumber = 0; roomNumber < ROOMS; roomNumber++ ) {
            strcpy(room[roomNumber].description, "");
        }
    
    
        /* opening file for reading */
        fp = fopen("descriptions.txt" , "r");
        if(fp == NULL) {
            perror("Error opening descriptions.txt file");
            return(-1);
        }
    
    
        // This loop reads through the descriptions.txt file and sets the
        // description text for each room.
        while(fgets(strDescription, sizeof(strDescription), fp) != NULL) {
            if (strncmp(strDescription, "ROOM", 4) == 0) {
                strncpy(roomString, strDescription+4, 3);
                roomString[3] = '\0';
                roomNumber = atoi(roomString);
    
    
                lineCount = 0;
                while(strcmp(strDescription, "}\n") != 0) {
                    fgets(strDescription, sizeof(strDescription), fp);
                    if (strcmp(strDescription, "}\n") != 0) {
                        if (lineCount == 0) {
                            strcpy(room[roomNumber].description, strDescription);
                        } else {
                            strcat(room[roomNumber].description, strDescription);
                        }
                        lineCount++;
                    }
                }
            }
        }
    
    
        fclose(fp);
    It works, but it feels less than elegant to me somehow. I'll try to think of a better method someday.
    Last edited by blixel; 03-07-2020 at 07:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Store Multiple result into a text file using fstream
    By yappy in forum C++ Programming
    Replies: 4
    Last Post: 07-30-2010, 08:33 PM
  2. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 11:27 AM
  3. Replies: 1
    Last Post: 03-27-2009, 04:21 AM
  4. read a text file with lines of variable length
    By raymond in forum C Programming
    Replies: 7
    Last Post: 06-24-2006, 06:41 PM
  5. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM

Tags for this Thread