Mail Archives: djgpp/2005/04/09/18:02:15
I'm having a problem saving a struct to disk using DJGPP. Normally I'd think
the problem was me but in view of the fact that it works under SOME
circumstances, I'm not sure. Here's the details.
Here's the struct:
/* -----------------12/24/04 1:46PM------------------
this is permanent structure and saved to disk with
file extension .KRP
--------------------------------------------------*/
#ifndef POWER_H /* these are rpm, torque, horsepower figures */
#define POWER_H
#ifndef GLOBAL_POWER
extern
#endif
/* #define MAX_RPM_PNTS 300 */
struct power {
char filename[12]; /* 8 + . + 3 */
unsigned short w; /* number of rpm points */
unsigned short n[300]; /* rpm */
float t[300]; /* torque */
float hp[300]; /* horsepower */
/* unsigned short n[MAX_RPM_PNTS]; */ /* rpm */
/* float t[MAX_RPM_PNTS]; */ /* torque */
/* float hp[MAX_RPM_PNTS]; */ /* horsepower */
char power_note_1[70];
char power_note_2[70];
char power_note_3[70];
char power_note_4[70];
char power_note_5[70];
char power_note_6[70];
} power_0 ;
typedef struct power POWER;
POWER power_0;
#endif
Here's the loop for acquiring data:
/* -----------------3/17/05 12:55PM------------------
this function gathers power data about bike
--------------------------------------------------*/
void data_get_power(POWER *ptr_power_0)
{
int max_len = 0;
char string[max_len];
int row_cnt = 0;
int col_cnt = 0;
int w = 0; /* counter for rpm points */
char sec_name[] = "Enter/Change RPM/Torque/Horsepower Data";
heading(sec_name);
clr_area(4,22,1,78);
locate(5,4);
printf("RPM TRQ HP");
locate(6,4);
printf("--- --- --");
locate(5,29);
printf("RPM TRQ HP");
locate(6,29);
printf("--- --- --");
locate(5,55);
printf("RPM TRQ HP");
locate(6,55);
printf("--- --- --");
row_cnt = 7;
col_cnt = 5;
w = 0;
locate(21,5);
printf("ENTER Q IN RPM FIELD TO QUIT .... ");
while (1)
{
get_input(row_cnt,col_cnt,string,5);
if ((*string == 'q') || (*string == 'Q'))
{
break;
}
ptr_power_0->n[w] = atoi(string);
/* this is for debugging */
clr_area(21,22,1,78);
locate(21,5);
printf("ptr_power_0->n[w] = %i", ptr_power_0->n[w]);
locate(21,50);
printf("w = %d", w);
get_input(row_cnt,col_cnt + 8,string,5);
if ((*string == 'q') || (*string == 'Q'))
{
break;
}
ptr_power_0->t[w] = atof(string);
/* this is for debugging */
clr_area(21,22,1,78);
locate(21,5);
printf("ptr_power_0->t[w] = %5.2f", ptr_power_0->t[w]);
locate(21,50);
printf("w = %d", w);
get_input(row_cnt,col_cnt + 17,string,5);
if ((*string == 'q') || (*string == 'Q'))
{
break;
}
ptr_power_0->hp[w] = atof(string);
/* this is for debugging */
clr_area(21,22,1,78);
locate(21,5);
printf("ptr_power_0->hp[w] = %5.2f", ptr_power_0->hp[w]);
locate(21,50);
printf("w = %d", w);
ptr_power_0->w = w;
w++;
row_cnt++;
if (row_cnt == 20 && col_cnt == 5)
{
row_cnt = 7;
col_cnt = 30;
}
if (row_cnt == 20 && col_cnt == 30)
{
row_cnt = 7;
col_cnt = 56;
}
if (row_cnt == 20 && col_cnt == 56)
{
clr_area(21,22,1,78);
locate(21,56);
printf("\a\aTHAT'S ALL FOLKS!");
locate(21,5);
printf("Press any key to continue ... ");
pause(0);
break;
}
}
clr_area(21,22,1,78);
locate(21,5);
printf("Press any key to continue ... ");
pause(0);
}
Here's the code for saving file:
/* -----------------3/20/05 12:32PM------------------
this function saves data about rpm, torque, horsepower
to file
--------------------------------------------------*/
void file_save_power(POWER *ptr_power_0)
{
char filename[12];
int max_len = 0;
char string[max_len];
FILE *ptr_file;
char sec_name[]="Save RPM/Torque/Horsepower Data To File";
heading(sec_name);
clr_area(4,22,1,78);
locate(5, 5);
printf("Enter FILE NAME (no extension) - ");
get_input(5, 45, string, 8);
strcpy(filename, string);
strcat(filename, ".KRP");
strcpy(ptr_power_0->filename, filename);
ptr_file = fopen(filename, "w");
if (ptr_file == NULL)
{
locate(20,5);
printf("ERROR OPENING FILE! ... ");
}
fwrite(&power_0, sizeof(power_0), 1, ptr_file);
fclose(ptr_file);
locate(20,5);
printf("Press any key to continue ... ");
pause(0);
}
Here's the code for retrieving file:
/* -----------------12/19/04 3:52PM------------------
this function opens file for reading
--------------------------------------------------*/
void file_read_power(POWER *ptr_power_0)
{
char filename[12];
int max_len = 0;
char string[max_len];
FILE *ptr_file;
char sec_name[]="Read RPM/Torque/Horsepower Data From File";
heading(sec_name);
clr_area(4,22,1,78);
locate(5, 5);
printf("Enter FILE NAME (no extension) - ");
get_input(5, 45, string, 8);
strcpy(filename, string);
strcat(filename, ".KRP");
ptr_file = fopen(filename, "r");
if (ptr_file == NULL)
{
locate(20, 5);
printf("ERROR OPENING FILE! ... ");
}
fread(&power_0, sizeof(power_0), 1, ptr_file);
fclose(ptr_file);
locate(20,5);
printf("Press any key to continue ... ");
pause(0);
}
Finally, here's the code for displaying file:
/* -----------------3/20/05 2:00PM-------------------
this function displays rpm, torque and horsepower
from current file
--------------------------------------------------*/
void data_display_power(POWER *ptr_power_0)
{
char sec_name[]="Display/Print RPM/Torque/Horsepower Data";
heading(sec_name);
int row_cnt = 0;
int col_cnt = 0;
int w = 0; /* counter for rpm points */
clr_area(4,22,1,78);
locate(5,4);
printf("RPM TRQ HP");
locate(6,4);
printf("--- --- --");
locate(5,29);
printf("RPM TRQ HP");
locate(6,29);
printf("--- --- --");
locate(5,55);
printf("RPM TRQ HP");
locate(6,55);
printf("--- --- --");
row_cnt = 7;
col_cnt = 5;
w = 0;
locate(4,5);
printf("Current Filename is - %s", ptr_power_0->filename);
while (w <= ptr_power_0->w)
{
locate(row_cnt, col_cnt);
printf("%u",ptr_power_0->n[w]);
locate(row_cnt, col_cnt+8);
printf("%5.2f",ptr_power_0->t[w]);
locate(row_cnt, col_cnt+17);
printf("%5.2f",ptr_power_0->hp[w]);
w++;
row_cnt++;
if (row_cnt == 20 && col_cnt == 5)
{
row_cnt = 7;
col_cnt = 30;
}
if (row_cnt == 20 && col_cnt == 30)
{
row_cnt = 7;
col_cnt = 56;
}
if (row_cnt == 20 && col_cnt == 56)
{
clr_area(21,22,1,78);
locate(21,56);
printf("\a\aTHAT'S ALL FOLKS!");
locate(21,5);
printf("Press any key to continue ... ");
pause(0);
break;
}
}
locate(21,5);
printf("Press any key to continue ... ");
pause(0);
Everything works fine entering from 1 to 3 data sets. When I enter a 4th
set, save and retrieve the file, the first 3 entries for RPM are correct, TRQ and
HP are all zeros and the 4th value for RPM is completely incorrect. This
situation occurs no matter how many data sets I enter, 4 or more. Like I said, 3
or less data sets work fine.
I did look inside the file and with four data sets, the bytes beyond 20 seem
to be haywire.
Any help would be appreciated.
Bill
********************************************************************
Keller Racing
Performance by Design
KellerRcng AT cs DOT com
http://www.bylleet.com/keller_racing
http://ourworld.compuserve.com/homepages/nostalgia_drag_bike
SUPPORT YOUR LOCAL DRAG RACER!!
********************************************************************
- Raw text -