简体   繁体   中英

strcpy giving me segmentation fault

I have code which is giving me a segmentation fault. I debugged it and and the error occurred when the strcpy executed. The code is attempting to extract data from a text file and store it into an array of structs. I plan to use strcpy to store the store the text file's data into the structs. Any idea why this is occurring?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int input( char *s, int length);

void main(){

    char *tok;
    char *buffer;
    size_t bufsize = 32;
    size_t characters;
    FILE *f;
    char *file_name;
    char line[255];
    int currentRoom;
    int count = 0;
    typedef struct {
        char room_n;
        char description[100];
        char room_north;
        char room_south;
        char room_west;
        char room_east;
    } room;

    //Creating an array of structs
    room record[1000];
    while(1){

        buffer = (char *)malloc(bufsize * sizeof(char));


        if(buffer == NULL){
            perror("Unable to allocate buffer");
            exit(1);
        }
        printf("Enter a command: ");
        characters = getline(&buffer, &bufsize, stdin);

        if(strcmp(buffer,"exit\n") == 0){
            printf("Exiting...\n");
            exit(1);
        }

        tok = strtok(buffer, " \n"); // Tokenize input
        printf("%s is the token \n", tok);


        if (strcmp(tok,"loaddungeon") == 0){
            file_name = strtok(NULL, "\n");
            printf("file name : %s \n", file_name);
            f = fopen(file_name,"r");
            while (fgets(line, sizeof(line), f) != NULL)
                {

                    char val1[128];
                    char val2[128];
                    char val3[128];
                    char val4[128];
                    char val5[128];
                    char val6[128];

                    strcpy(val1, strtok(line, "$"));
                    strcpy(val2, strtok(NULL, "$"));
                    strcpy(val3, strtok(NULL, " "));
                    strcpy(val4, strtok(NULL, " "));
                    strcpy(val5, strtok(NULL, " "));
                    strcpy(val6, strtok(NULL, " "));

                    //Segmentation fault error occurs here
                    strcpy(record[count].room_n, val1);

Definition of strcpy() is:

char *strcpy(char *dest, const char *src)

where-

  • dest -- This is the pointer to the destination array where the content is to be copied.

  • src -- This is the string to be copied.

In your code arguments passed to strcpy() is char and char * :

strcpy(record[count].room_n, val1);

As defined in the structure:

typedef struct {
        char room_n;  //room_n declared as 'char'
        char description[100];
        char room_north;
        char room_south;
        char room_west;
        char room_east;
    } room;

Suggestion:

Allocate memory for room_n to point to. Change the declaration to char room_n[128];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM