Δημοσιεύτηκε: 06 Οκτ 2011, 21:31
Δεν ξέρω αν σε βοηθάει καθόλου, αλλά σου έγραψα ένα δομημένο κώδικα που διαβάζει ένα αρχείο σαν αυτό που θέλεις, το βάζει στη μνήμη σε έναν πίνακα table, τον οποίο και τυπώνει στο τέλος. Θέλει compiler που να υποστηρίζει C99 standard ( -std=c99 στον gcc ).
- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXINBUF 256+1
#define MAX_RECORDS 100
#define MAXSLEN_CITY (31+1) // max string length of a city
#define MAXSLEN_DATE (15+1) // max string length of a date
#define MAXSLEN_WIND (3+1) // max string length of wind
#define MAX_DATETOKENS 3 // max tokens in a date string
#define MASXLEN_DATETOKEN (6+1) // max string length of a date token
#define private
#define pressENTER() \
do { \
printf("\npress ENTER..."); \
while ( getchar() != '\n' ) \
; \
} while (0)
typedef enum Bool { FALSE=0, TRUE } Bool;
typedef struct Date { // Date Structure
char datestr[ MAXSLEN_DATE ]; // ... date as string
private int day, month, year; // ... privately converted to ints
} Date;
typedef struct Record { // Record Structure
int key;
char citystr[ MAXSLEN_CITY ]; // ... string for the city
Date date; // ... Date structure for the date
int temper; // ... int for the temperature
int humid; // ... int for the humidity
char windstr[ MAXSLEN_WIND ]; // ... string for the wind
} Record;
/* -------------------------------------------------------------------------------------
* int s_tokenize(char *s, char *tokens[], int maxtokens, char *delimiters)
*
* Split c-string s to up to maxtokens, using chars in delimiters as separators and
* saving each token into the array tokens[].
*
* Return number of split tokens or 0 on error.
* -------------------------------------------------------------------------------------
*/
int s_tokenize(char *s, char *tokens[], const int maxtokens, const char *delimiters)
{
register int i=0;
if ( !s || !tokens || !delimiters )
return 0;
tokens[0] = strtok(s, delimiters);
if (tokens[0] == NULL)
return 0;
for (i=1; i < maxtokens && (tokens[i]=strtok(NULL, delimiters)) != NULL; i++)
;
return i;
}
// -----------------------------------------------------------------------------------
// Print the day, month and year integer fields of a date structure
// -----------------------------------------------------------------------------------
Bool date_printints( const Date *date )
{
if ( !date || date->day == 0 || date->month == 0 || date->year == 0 )
return FALSE;
printf("%d-%d-%d\n", date->day, date->month, date->year );
return TRUE;
}
// -----------------------------------------------------------------------------------
// Convert a c-string datestr into its corresponding day, month & year integer fields,
// inside a data structure
// -----------------------------------------------------------------------------------
Bool date_str2ints( Date *date, char *datestr )
{
register int i = 0;
char tempstr[ MAXSLEN_DATE ] = {'\0'};
char *strtokens[ MAX_DATETOKENS ];
if ( !date || !datestr)
return FALSE;
strncpy( tempstr, datestr, MAXSLEN_DATE-1 );
if ( s_tokenize( tempstr, strtokens, MAX_DATETOKENS, "/-" ) < MAX_DATETOKENS )
return FALSE;
for (i=0; i < MAX_DATETOKENS; i++)
{
date->day = atoi( strtokens[0] );
date->month = atoi( strtokens[1] );
date->year = atoi( strtokens[2] );
}
return TRUE;
}
// -----------------------------------------------------------------------------------
// Convert the day, month & year integer fields of a date structure into a c-string
// -----------------------------------------------------------------------------------
Bool date_ints2str( char *datestr, const size_t maxslen, const Date *date )
{
if ( !date || !datestr )
return FALSE;
snprintf( datestr, maxslen, "%d/%d/%d", date->day, date->month, date->year );
return TRUE;
}
// -----------------------------------------------------------------------------------
// print the fields of a rec structure
// -----------------------------------------------------------------------------------
Bool record_print( const Record *rec )
{
if ( !rec )
return FALSE;
if ( rec->key != 0 )
{
printf("city: %s\n", rec->citystr);
printf("date: %s\n", rec->date.datestr);
printf("temper: %d\n", rec->temper);
printf("humid: %d\n", rec->humid);
printf("wind: %s\n\n", rec->windstr);
}
return TRUE;
}
// -----------------------------------------------------------------------------------
// print up to maxrecords records of table
// -----------------------------------------------------------------------------------
Bool table_print( const Record table[], const int maxrecords )
{
register int i = 0;
if ( maxrecords > MAX_RECORDS )
return FALSE;
for (i=0; i < maxrecords; i++)
record_print( &table[i] );
return TRUE;
}
// -----------------------------------------------------------------------------------
int main( void )
{
char inbuf[ MAXINBUF ] = {'\0'};
FILE *infile = NULL;
Record table[ MAX_RECORDS ];
register int i = 0;
// open the file
infile = fopen("myfile.txt", "r");
if ( infile == NULL ) {
printf("Error code 1: File does not exist!\n");
pressENTER();
exit( 1 );
}
// zero all records in table
memset( table, 0, MAX_RECORDS * sizeof( Record ) );
// read up to MAX_RECORDS records from infile into table (one rec per line)
for ( i=0; i < MAX_RECORDS && fgets( inbuf, MAXINBUF, infile ); i++ )
{
sscanf( inbuf,
"%s %s %d %d %s",
table[i].citystr, table[i].date.datestr, &table[i].temper,
&table[i].humid, table[i].windstr
);
// convert date string to corresponding integer fields
if ( !date_str2ints( &table[i].date, table[i].date.datestr) )
{
puts("Error code 2: invalid date found in file!\n");
fclose( infile );
pressENTER();
exit( 2 );
}
// set the record's key
table[i].key = i+1;
}
table_print( table, MAX_RECORDS );
fclose( infile );
printf("teleiwse kanonika!!! \n");
pressENTER();
exit( 0 );
}