我是 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/