Pregunta

this is an assignment for my CS course, im trying to write a code that reads a file line by line and put the input into a struct element.the struct looks like this:

typedef char* Name;
struct Room
{
    int     fStatus;
    Name    fGuest;
};

the status is 0 for available and 1 for booked. the name will be empty if the room is available.

there are 2 function, one to read and put the values to a struct element, and the other one to print it out.

int openRoomFile()
{
    FILE *roomFile;
    char *buffer = NULL;
    size_t length = 0;
    size_t count = 0;

    roomFile = fopen("roomstatus.txt", "r+");
    if (roomFile == NULL)
        return 1;

    while (getline(&buffer, &length, roomFile) != -1) {
        if (count % 2 == 0) {
            sscanf(buffer, "%d", &AllRooms[count].fStatus);
        } else {
            AllRooms[count].fGuest = buffer;
        }
        count++;
    }

    fclose(roomFile);
    free(buffer);
    return 0;
}

print function

void printLayout(const struct Room rooms[])
{
    for (int i=0; i<3; i++) {
        printf("%3d \t", rooms[i].fStatus);
        puts(rooms[i].fGuest);
    }
}

the output is not what i expected, given the input file is :

1
Johnson
0

1
Emilda

i will get the output :

1   (null)
0   
0   (null)

i dont know what went wrong, am i using the right way to read the file? every code is adapted from different sources on the internet.

¿Fue útil?

Solución

Here is a fixed version of the openRoomFile()

int openRoomFile(void)
{
    FILE *roomFile;
    char *buffer = NULL;
    size_t length = 0;
    size_t count = 0;

    roomFile = fopen("roomstatus.txt", "r+");
    if (roomFile == NULL)
        return 1;

    while (1) {
        buffer = NULL;
        if (getline(&buffer, &length, roomFile) == -1) {
            break;
        }
        sscanf(buffer, "%d", &AllRooms[count].fStatus);
        free(buffer);

        buffer = NULL;
        if (getline(&buffer, &length, roomFile) == -1) {
            fprintf(stderr, "syntax error\n");
            return 1;
        }
        AllRooms[count].fGuest = buffer;
        count++;
    }

    fclose(roomFile);
    return 0;
}

When you no longer need those fGuest anymore, you should call free on them.

Otros consejos

If your input is guaranteed to be valid (as were many of my inputs in my CS classes), I'd use something like this for reading in the file.

while(!feof(ifp)){
    fscanf(ifp,"%d%s",&AllRooms[i].fStatus, AllRooms[i].fGuest); //syntax might not be right here
                                                                //might need to play with the '&'s
                                                                //and maybe make the dots into
                                                                //arrows

     //do work here
    i++;
}

You are not allocating memory for Name. Check this. In the below example i'm not included free() calls to allocated memory. you need to call free from each pointer in AllRooms array, once you feel you are done with those and no more required.

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

typedef char* Name;
struct Room
{
    int     fStatus;
    Name    fGuest;
}Room_t;
struct Room AllRooms[10];

int openRoomFile()
{
    FILE *roomFile;
    char *buffer = NULL;
    size_t length = 0;
    size_t count = 0;
    size_t itemCount = 0;

    roomFile = fopen("roomstatus.txt", "r+");
    if (roomFile == NULL)
        return 1;
    buffer = (char *) malloc(16); // considering name size as 16 bytes
    while (getline(&buffer, &length, roomFile) != -1) {
        if (count % 2 == 0) {
            sscanf(buffer, "%d", &AllRooms[itemCount].fStatus);
        } else {
            AllRooms[itemCount].fGuest = buffer;
        itemCount++;
        }
        count++;
    buffer = (char *) malloc(16); // considering name size as 16 bytes
    }

    fclose(roomFile);
    free(buffer);
    return 0;
}
void printLayout(const struct Room rooms[])
{
    int i;
    for (i=0; i<3; i++) {
        printf("%3d \t", rooms[i].fStatus);
        puts(rooms[i].fGuest);
    }
}

int main(void)
{
  openRoomFile();
  printLayout(AllRooms);
  // free all memory allocated using malloc()
  return 0;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top