该程序使用一个称为island.txt的外部文件,并从每个订单项中构建一个链接列表。问题是,在撰写本文时,我无法正确复制该结构,它仅指向创建的当前结构的指针。你们可以让该程序打印“结果:成功!! ”吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* The data format of an island defined within a linked-list format
*/
typedef struct island {
char * name;
char * opens;
char * closes;
struct island * next;
} island;
/**
* The island create method
*/
island * create ( char * name ){
island * country = malloc(sizeof(island)); // Allocate a memory on the heap for the country
country->name = strdup(name); // Allocate a memory on the heap for the name
country->opens = "6:00 AM";
country->closes = "11:00 PM";
country->next = NULL;
free(country);
return country;
}
/**
* Automatically link islands togerther forming a link list on-new-line event
*/
void autoLink( char * country_name, char * file_name ){
FILE * islands; // Define islands as a file
islands = fopen(file_name, "r");
island * start_country = NULL; // An island variable to record the 1st item of the linked list
island * country = NULL; // The next country item of an island
island * previously_created_country = NULL;
if(islands == NULL){
perror("Error opening the file");
}
else{
char previous_country_name[80]; // The previous island name
fgets(previous_country_name, 80, islands);
while(fgets(country_name, 80, islands) != NULL){
country = create(country_name);
if(start_country == NULL){ // When starting the loop the next country is the start (1st country)
start_country = country;
}
else{
previously_created_country = create(previous_country_name);
previously_created_country->next = country;
printf("\nThe current name is: %s", country_name);
printf("Country name inside struct: %s", country->name);
printf("The previously created country name inside the struct: %s", previously_created_country->name);
printf("The previous name is: %s", previous_country_name);
if(strcmp(country_name, country->name)){
printf("*****Result: Country name error!!!*****\n");
}
if(strcmp(previously_created_country->name, previous_country_name)){
printf("*****Result: Previous Country name error!!!*****\n");
}
if(!(strcmp(country_name, country->name) && strcmp(country_name, country->name))){
printf("*****Result: Success!!!*****\n");
}
}
strcpy(previous_country_name, country_name);
}
}
}
/**
* Starting point
*/
int main () {
char name[80]; // The name of a given island
char file_name[30] = "islands.txt";
autoLink(name, file_name);
return 0;
}
这是islands.txt文件的内容:
Haiti
Anguilla
Antigua and Barbuda
Aruba
The Bahamas
Barbados
Bonaire
British Virgin Islands
Cayman Islands
Cuba
Curaçao
Dominica
Dominican Republic
Federal Dependencies of Venezuela
Grenada
Guadeloupe
Jamaica
Martinique
Montserrat
Navassa Island
Nueva Esparta
Puerto Rico
Saba
San Andrés and Providencia
Saint Barthélemy
Saint Kitts and Nevis
Saint Lucia
Saint Martin
Saint Vincent and the Grenadines
Sint Eustatius
Sint Maarten
Trinidad and Tobago
Turks and Caicos Islands
United States Virgin Islands
在您的create
职能:
island * create ( char * name ){
island * country = malloc(sizeof(island)); // Allocate a memory on the heap for the country
country->name = strdup(name); // Allocate a memory on the heap for the name
country->opens = "6:00 AM";
country->closes = "11:00 PM";
country->next = NULL;
free(country); // <---- BAD
return country;
}
完成填充对象后,将free
其放回并返回释放的指针。由于释放了内存,该内存不再是值,并且尝试使用指向已释放内存的指针会调用未定义的行为。
摆脱free
此功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句