Can anyone help me to improve on my coding techniques by helping me to rewrite this program in a better way?
This is a discussion on Anyone help me write this program better? within the C Programming forums, part of the General Programming Boards category; Can anyone help me to improve on my coding techniques by helping me to rewrite this program in a better ...
Can anyone help me to improve on my coding techniques by helping me to rewrite this program in a better way?
An example of the input file is attached below!
The file is a text file and each line is terminated by a newline character!Code:I100140100060002 I100060100060001 I100220128070008 I100490121060099 I100650544020100 C10081COVER SYSTEMS LTD ALBOY RD;KINVER;SOUTH STAFFS; ; 0002345000010000 I101030201090507 I100060493010009 I130050303090007 I140010134040044 I200010224030202 I222250160040032 I117030354080090 I117030544020300 R1986X0121060200 I199840101030085 I1518X0125050002 I1518X0140010002 D31003 I191860133070090 I193210215040088 I100060350090009 I1932103530X0092 I193210414080077 I1332305200X0250 I1454X0110020066 I1454X0142069999 R1454X0214070099 I197800522050100 R197800500080075 I197800415050055 I100220301040040 I100060302010030 I100060141090100 C10367MAXIMES PAINT STORESWATERLOO TERRACE;BRIDGNORTH;STAFFS; ; 0086575000099999 I1036702040X0333 I107400211050332 I1074001180X0003 I133400203030003 I133400211050012 I133400140010012 C19925CHUCKERS DIY 19 HIGH STREET;STOURBRIDGE;WEST MIDS; ; 0000000000000000 I1258X01180X0102 I1258X0116061234 I112820210080065 I112820224030055 I134040116060009 I134040117030009 D13404 I1116903480X0008 I111690404010007 R100060542160552 I103830307080045 I103830313050035 I103670505040200 R103670534060100 I103240410090015 I103240310030015 I135440526040020 I135440121060010 I136250148000005 I124670114010045 I124670110020675 I198440130050456 I198440302010790 D13358 I1308X0117200122 I1308X0354080033 I100060400020067 I100060401000056 I100060522050555 C10219SPLASH AND DRIBBLE 33 THE POLES;ALAWAY HILL;BIRMINGHAM;WEST MIDS; 0000100000008000 I183090342070044 I183090505040023 I183090309020010 I1812X0351060222 I1812X0133070123 R1000601390X0067 R100060134040099 I100060132000088 I145240117620006 I138890114010004 I138890100490003 R192240212020032 I192240304060100 I192240346060067 I100060342230002 I100060344010002 I189290526040099 I189290532010062 I120090342070062 I151050349080044 I151050350090004 I112820141090004 R112820221010678 R107400302010052 I107400210080345 I107400132000567 R101620120090042 R101620111262995 I101620401000003 I101200504070002 I101200414080002 R200100404010009 R100060403040099 I100060530070088 I100060307080004 D10022 I101200344010009 R1012003110X0067 R101200134040004 R101620205080002 R101620145080001 I101620213000005 R101620114010042 C13218TRUE COLOURS THE CORBETT CENTRE;OLDBURY;WEST MIDS; ; 0000500590001500 I101890112070032 I101890104050010 D11525 I100060214070009 R100060403040402
Avoid terminating your if() statements.Check return values.Code:if(fgets(string,104,input_ptr)); { /* ... */ }Be sure not to go beyond the bounds of arrays.Code:database = malloc(num_of_recs * (sizeof(union records))); /* no check of (database != NULL) before database is dereferenced */Avoid magic numbers and reserved words (string).Code:/* ... */ cust_code[6], /* ... */ cust_limit[8]; /* ... */ database[x].c.cust_code[6]='\0'; /* ... */ database[x].c.cust_limit[8]='\0';Look up the function strncpy() and consider it as a replacement for the loops.Code:char string[104]; /* ... */ while(fgets(string,104,input_ptr)) { /* ... */ } /* instead consider*/ char buffer[104]; while(fgets(buffer,sizeof(buffer),input_ptr)) { /* ... */ }And again consider sizeof() instead of hard-coding the size in.Code:cust_bal[10], /* ... */ for(y=0;y<9;y++) database[x].c.cust_bal[y]=string[y+86]; database[x].c.cust_bal[9]='\0';
Some functions have misleading names. For example, atof returns a double.Might you want bal_total to be a bal_double?Code:float bal_total=0; /* ... */ bal_total+=((atof(database[x].c.cust_bal))/100);
[EDIT]
Remember to free malloc'd memory.
For the following,why notCode:int sort_function(const void *a, const void *b) { const union records *pa = a; const union records *pb = b; if (strncmp( pa->c.cust_code, pb->c.cust_code, 5) < 0) return -1; if (strncmp( pa->c.cust_code, pb->c.cust_code, 5) > 0) return +1; /*else they are equal*/ return (pb->c.type - pa->c.type); }Why have database and database2? You are using x to index into the database array, so you could reuse database after the sort.Code:int sort_function(const void *a, const void *b) { const union records *pa = a; const union records *pb = b; return strncmp(pa->c.cust_code, pb->c.cust_code, sizeof(pa->c.cust_code) - 1); }
[/EDIT]
[EDIT]
Reminded by a recent thread on exit(), you may also want to use exit(EXIT_FAILURE) instead of exit(1) when fopen() fails. And adding a perror() to the failure condition might provide a better reason for the failure. Whether or not you choose to use perror(), you might consider attaching an identifier to the file name strings.Then you could have something likeCode:const char InputFilename[] = "503442VF.dat"; const char OutputFilename[] = "503442SF.dat"; const char PrintFilename[] = "PRINT.DAT";Using offsetof() instead a hard-coded offset (in the case of string[y+86], for example) was another idea. But since strctures may contain padding that the text file might not match, this could be bad. But if your compiler and options could circumvent padding, then it might be possible to encapsulate these values using the structure definition itself.Code:input_ptr= fopen(InputFilename,"rt"); if ( input_ptr == NULL ) { printf("\nError in opening input file!!"); perror(InputFilename); exit(EXIT_FAILURE); }
Other thoughts might be creating structure-specific parsing routines -- as separate functions -- to be called once the type is evaluated. Or maybe fscanf() would be useful for parsing the input file.
Other thoughts might be to use a switch on string[0] instead of successive if comparisons, to minimize repetition in the C code as a maintenance concern.
You might have print_headers return the number of lines to increment. That waycould be done asCode:print_headers(page_num); line_count+=4;[/EDIT]Code:line_count+=print_headers(page_num);
Last edited by Dave_Sinkula; 11-28-2002 at 04:13 AM.