Hi Dave,
I was hoping to make a program that would be easier for the next maintenace programmer to follow,
I have an enumeration
Code:
#ifndef CIRP_ENUMERATION_H
#define CIRP_ENUMERATION_H
enum
{
COUNTRY,
REGION_CODE,
REGION_NAME,
... 84 in total
a structure
Code:
#ifndef CIRP_DATA_EXTRACT_OUTPUT_LAYOUT_H
#define CIRP_DATA_EXTRACT_OUTPUT_LAYOUT_H
#include "cirp_data_extract_output_field_sizes.h"
typedef struct {
char country [O_COUNTRY_LEN];
char region_code [O_REGION_CODE_LEN];
char region_name [O_REGION_NAME_LEN];
... 84 in total
a set of constants for the field sizes
Code:
#ifndef CIRP_DATA_EXTRACT_OUTPUT_FIELD_LENGTHS_H
#define CIRP_DATA_EXTRACT_OUTPUT_FIELD_LENGTHS_H
#define O_COUNTRY_LEN 80
#define O_REGION_CODE_LEN 20
#define O_REGION_NAME_LEN 80
... 84 in total
and a program that opens a delimited text file and works on it.
The last process it does at present, is run through the current output file and change it from being delimited to fixed length
Code:
void create_smartbills(FILE ** fp)
{
RecordOut out_file;
int loop;
char buffer[BUFFER_SIZE];
char * start;
char * end;
int field_number = COUNTRY;
int count = 0;
#ifdef DEBUG
printf("In create_smartbills().\n");
fflush(NULL);
#endif
/* count the number of lines in the file */
count = get_count(fp, FIN_FILE);
rewind(fp[FIN_FILE]);
for(loop = 0; loop < count; loop++)
{
while(fgets(buffer, BUFFER_SIZE, fp[FIN_FILE]))
{
if(*buffer == EOF || *buffer == NULL)
{
break;
}
else
{
start = buffer;
/* initialise the output record to spaces */
memset(&out_file, SPACE_INT, sizeof(out_file));
while(*start)
{
/* find the terminating semi-colon */
for(end = start; * end != '\n' && * end != SEPARATOR; end++);
/* terminate the string */
*end = 0;
/* copy the field to the output record */
set_output_field(&out_file, start, field_number);
/* advance to the next field */
start = end + 1;
field_number++;
/* reset the enum value for each record */
if(field_number == LAST_FIELD)
{
/* first field */
field_number = COUNTRY;
}
}
fwrite(&out_file, sizeof(out_file), 1, fp[DELTA_FILE]);
fwrite("\n", 1, 1, fp[DELTA_FILE]);
}
}
}
#ifdef DEBUG
printf("Out create_smartbills().\n");
fflush(NULL);
#endif
}
int get_count(FILE ** fp, int text_file)
{
int ch;
int count = 0;
#ifdef DEBUG
printf("In get_count().\n");
fflush(NULL);
#endif
/* helper function to count files already written */
fseek(fp[text_file], 0, SEEK_SET);
while((ch = fgetc(fp[text_file])) != EOF)
if(ch == '\n')
count++;
return count;
#ifdef DEBUG
printf("Out get_count().\n");
fflush(NULL);
#endif
}
void set_output_field(RecordOut * output, char * text, int field_number)
{
/* Long enough for the longest int */
char temp_buffer[128];
#ifdef DEBUG
printf("In set_output_field().\n");
fflush(NULL);
#endif
switch(field_number)
{
case COUNTRY:
memcpy(output->country, text, strlen(text));
break;
case REGION_CODE:
memcpy(output->region_code, text, strlen(text));
break;
case REGION_NAME:
memcpy(output->region_name, text, strlen(text));
break;
... 84 in total
and after a week, that is the best I have come up with so far, my time is running out here, but I will always gladly listen to advice,
tia,