Code:
//insert_database: insert ctry into the right element of db list
void insert_database( struct Database * db, struct Country * ctry )
{
int i, insertAt = 0;
if( db != NULL && ctry != NULL )
{
for( i = 0; i < db->ncountries; i++ )
{
if( /*validate location*/ )
insertAt = i;
}
//printf( "Insert at: %d\n", insertAt );
copy_country( db, ctry, insertAt );
}
}
The rest of the code, if needed:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Country
{
int continent;
char * name;
char * cap;
char * lang;
};
struct Database
{
struct Country * list;
int ncountries;
char * filename;
};
/***** Function Prototypes *****/
struct Database * init_database( char filename[] );
void fill_database( struct Database * db );
struct Country * init_country( int continent, char * name, char * capital, char * languages );
void insert_database( struct Database * db, struct Country * ctry );
void copy_country( struct Database * db, struct Country * ctry, int elem );
void print_database( struct Database * db );
void print_country( struct Country * country );
void delete_database( struct Database * db );
void delete_country( struct Country * ctry );
void FileHandlingFailed( const char * filename );
/***** Function Definitions *****/
int main()
{
struct Database * db;
if( (db = init_database( "lab.txt" )) != NULL )
{
fill_database( db );
//print_database( db );
//delete_database( db );
//free( db );
}
else
{
printf( "Memory allocation procedure failed. Application closing...\n\n" );
return 1;
}
return 0;
}
//init_database: initialize the Database struct
struct Database * init_database( char filename[] )
{
FILE * input;
struct Database * db;
int countries = 0, i;
char c;
if( (input = fopen( filename, "r" )) != NULL )
{
while( (c = fgetc( input )) != EOF )
{
if( c == '\n' )
countries++;
}
db = (struct Database *)malloc( (countries * sizeof( struct Country )) );
if( db != NULL )
{
db->list = (struct Country *)calloc( countries, sizeof( struct Country ) );
if( db->list != NULL )
{
db->ncountries = countries;
db->filename = filename;
}
}
fclose( input );
}
else
{
FileHandlingFailed( filename );
return NULL;
}
return db;
}
//fill_database: fills the Database country list with stuff from file
void fill_database( struct Database * db )
{
FILE * input;
struct Country * tempCtry;
int cont;
char name[81], capital[81], lang[81];
char c;
if( (input = fopen( db->filename, "r" )) != NULL )
{
while( fscanf( input, "%d,%[^,],%[^,],%[^\n]", &cont, name, capital, lang ) != EOF )
{
if( (tempCtry = init_country( cont, name, capital, lang )) != NULL )
insert_database( db, tempCtry );
else
{
printf( "Memory allocation failed in init_country function. Aborting...\n" );
break;
}
}
fclose( input );
}
else
{
FileHandlingFailed( db->filename );
return;
}
}
//init_country: allocates memory for a new Country structure and its contents
struct Country * init_country( int continent, char * name, char * capital, char * languages )
{
struct Country * ctryStruct;
ctryStruct = (struct Country *)malloc( sizeof( struct Country ) );
if( ctryStruct != NULL )
{
//country continent
ctryStruct->continent = continent;
//init name ptr
ctryStruct->name = (char *)calloc( strlen( name ) + 1, sizeof( char ) );
if( ctryStruct->name != NULL )
{
//copy in ctry name
strcpy( ctryStruct->name, name );
//init cap ptr
ctryStruct->cap = (char *)calloc( strlen( capital ) + 1, sizeof( char ) );
if( ctryStruct->cap != NULL )
{
//copy in ctry capital
strcpy( ctryStruct->cap, capital );
//init lang ptr
ctryStruct->lang = (char *)calloc( strlen( languages ) + 1, sizeof( char ) );
if( ctryStruct->lang != NULL )
{
//copy in ctry languages
strcpy( ctryStruct->lang, languages );
//printf( "%d, %s, %s, %s\n", ctryStruct->continent, ctryStruct->name, ctryStruct->cap, ctryStruct->lang );
}
else
{
free( ctryStruct->cap );
free( ctryStruct->name );
free( ctryStruct );
ctryStruct = NULL;
}
}
else
{
free( ctryStruct->name );
free( ctryStruct );
ctryStruct = NULL;
}
}
else
{
free( ctryStruct );
ctryStruct = NULL;
}
}
return ctryStruct;
}
//insert_database: insert ctry into the right element of db list
void insert_database( struct Database * db, struct Country * ctry )
{
int i, insertAt = 0;
if( db != NULL && ctry != NULL )
{
for( i = 0; i < db->ncountries; i++ )
{
if( /*validate location*/ )
insertAt = i;
}
//printf( "Insert at: %d\n", insertAt );
copy_country( db, ctry, insertAt );
}
}
//copy_country: performs insertion of the ctry struct into the db list
void copy_country( struct Database * db, struct Country * ctry, int elem )
{
db->list[elem] = *ctry;
}
//print_database: prints all the Countries
void print_database( struct Database * db )
{
int i;
for( i = 0; i < db->ncountries; i++ )
{
print_country( db->list+i );
}
printf( "Total countries: %d\n", db->ncountries );
return;
}
//delete_database: frees allocated memory in array of Countries
void delete_database( struct Database * db )
{
int i;
for( i = 0; i < db->ncountries; i++ )
{
delete_country( db->list+i ); // free mem alloc to a country
}
free( db->list ); // free array
free( db->filename );
}
//delete_country: frees memory for Country structures
void delete_country( struct Country * ctry )
{
free( ctry->name );
free( ctry->cap );
free( ctry->lang );
}
//print_country: prints a Country structure
void print_country( struct Country * country )
{
printf("%-23s%-20s%-25s\n", country->name, country->cap, country->lang );
}
//FileHandlingFailed: prints the error msg when program fails to load file
void FileHandlingFailed( const char * filename )
{
printf( "File handling procedure failed: unable to locate file %s. Application closing...\n\n", filename );
}
I've tried a few different things to validate the list index, but nothing works. Compiler won't let me do these things: