打开文件会在程序中显示段错误

时间:2016-01-17 17:48:03

标签: c segmentation-fault codeblocks

我是C程序的新手,我在理解如何使用fopen,fseek,freopen函数以及如何读取/写入链接列表到文件方面遇到了问题。

以下代码来自:

struct data used_servers(){
    struct data server_settings;

    s_type *head;
    s_type *actual;
    int r_id;
    char r_host[16];
    char r_port_inc[8];
    FILE *f;
    long size;
    head = NULL;

    char select_inc[10];
    int select;
    int count = 0;

    f=fopen("./config/config.init", "r");

    fseek(f, 0, SEEK_END);
    size = ftell(f);
    if(size==0){
        printf(" There are no servers to choose from. Proceeding to set up a new connection!\n");
        server_settings = initialize_server();

        head=actual=(s_type*)malloc(sizeof(s_type));
        actual->id=1;
        strcpy(actual->host,server_settings.host);
        strcpy(actual->port_inc,server_settings.port_inc);

        f=freopen("./config/config.init", "w", stdout);
        fprintf(f, "%d\n%s\n%s\n\n",actual->id, actual->host, actual->port_inc);
        fclose(f);

        return server_settings;
    }

    while(fscanf(f,"%d\n%s\n%s\n\n",&r_id,r_host,r_port_inc)!=EOF){
        if(head==NULL) head=actual=(s_type*)malloc(sizeof(s_type));
        else actual=actual->next=(s_type*)malloc(sizeof(s_type));
        actual->next=NULL;
        actual->id=r_id;
        strcpy(actual->host,r_host);
        strcpy(actual->port_inc,r_port_inc);
    }

    printf(" List of available servers: \n");
    for(actual=head;actual!=NULL;actual=actual->next){
        printf("  #%d - %s:%s\n", actual->id, actual->host, actual->port_inc);
    }

    printf("Please choose one of the following servers or set up a new one (ID,New): ");
    fgets(select_inc, sizeof(select_inc), stdin);

    if(strncmp(select_inc,"New",3)==0){
        printf("Setting up a new connection!\n");
        server_settings = initialize_server();
        while (actual->next!=NULL){
            actual = actual->next;
        };
        actual->next=(s_type*)malloc(sizeof(s_type));
        actual=actual->next;
        actual->id=r_id+1;
        strcpy(server_settings.host,actual->host);
        strcpy(server_settings.port_inc,actual->port_inc);

        actual=head;
        f=freopen("./config/config.init", "w", stdout);
        while(actual){
            fprintf(f, "%d\n%s\n%s\n\n",actual->id, actual->host, actual->port_inc);
            actual = actual->next;
        }
        printf("New server configuration was saved in config.init!\n");
        fclose(f);

        return server_settings;
    }

    const char *tmp=select_inc;
    while(isdigit(*tmp) && *tmp++);

    actual=head;
    while(actual!=NULL){
        actual=actual->next;
        count++;
    }

    if (*tmp=='\0'){
        select = atoi(select_inc);
        while (1){
            if(select>count){
                printf("Not a valid server. Please try again!: ");
                fgets(select_inc, sizeof(select_inc), stdin);
            }
            if(select<=count){
                break;
            }
        }

        actual=head;
        count=0;

        while (actual != NULL){
            if (count == select){
                strcpy(server_settings.host,actual->host);
                strcpy(server_settings.port_inc,actual->port_inc);
                break;
            }
            count++;
            actual=actual->next;
        }
    }
    fclose(f);
    return server_settings;
}

以下是我的结构声明

struct data{
    char host[16];
    char port_inc[8];
};

typedef struct s_list{
    int id;
    char host[16];
    char port_inc[8];
    struct s_list *next;
} s_type;

1 个答案:

答案 0 :(得分:3)

您刚刚误用freopen

这一行:

f=freopen("./config/config.init", "w", stdout);
  

freopen以其他模式重新打开已打开的文件。

你打开./config/config.init文件然后传递stdout作为打开的文件???

您只需传递f

所以代码是:

f=freopen("./config/config.init", "w", f);

这只是我看到的一个错误,您可能会有更多错误!

希望它有所帮助。