c - 打开文件会导致程序出现段错误

标签 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;

最佳答案

您刚刚误用了 freopen

这一行:

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

freopen reopens an opened file in another mode.

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

你只需传递f

所以代码是:

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

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

希望有帮助。

关于c - 打开文件会导致程序出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34841537/

相关文章:

c++ - 如何在 Linux 上的 IDE 中使用 C++ 代码的文档?

c++ - 无法在赋值中将 'ArithProgression*' 转换为 'Progression*'

c - 如何写出标记化的字符串

objective-c - 点 (".") 运算符和箭头 ("->") 运算符在 C 与 Objective-C 中的使用

c - 在 linux 主机上遇到 "floating point exception"错误

c - while循环中的段错误

c - 如何从字符串中扫描未知数量的元素?

c++ - 通过 for 循环对 2D vector 进行 Push_back

c - 使用 int 指针参数时 C 中的段错误

c - 创建 pthread 时如何正确调用函数?