I'm trying to load a flat fixed length file into a structure and I'm getting some garbage in the data fields.
Right now, I'm just trying to print to the screen the data that is in the file.
Eventually, I wanted to then connect to a database and run queries so that I could loop thru and load this data from the file into the appropriate table.
Can you look thru and see what I'm doing wrong.
Thanks in Advance
John
Here's my file definition and my main():
--------------------------------------------
struct record {
char bnumber[ 20 ];
char last_act_date[ 16 ];
char last_act_user[ 12 ];
char school_yr[ 4 ];
char work_level[ 1 ];
char agency[ 5 ];
char program[ 3 ];
char bill [1];
char fund_type[ 3 ];
char fund[ 3 ];
char account[ 2 ];
char fed_yr[ 4 ];
char object[ 1 ];
char sub_object[ 1 ];
char stat_sub_obj[ 3 ];
char priority[ 1 ];
char status[ 1 ];
char neg_code[ 3 ];
char transaction[ 5 ];
char req_type[ 1 ];
char rec_amt[ 20 ];
char cash_impact[ 20 ];
char desc[ 80 ];
char approp_rec_num[ 20 ];
char cat[ 2 ];
char order[ 4 ];
char type[ 1 ];
char workforce[ 12 ];
};
int main()
{
FILE *source;
int n_bytes = 0;
struct record cuts;
source = fopen("CUTS_2002.TXT","r"
if(!source)
{
printf("no data file found\n"
exit(0);
8 }
while (0 != (n_bytes = fread(&cuts, 1, sizeof(struct record), source)) )
{
printf("Bytes Read: %d\n", n_bytes);
printf("%s %s %s %s %s\n",
cuts.bnumber,
cuts.agency,
cuts.program,
cuts.bill,
cuts.fund);
fread(&cuts, 1, sizeof(struct record), source);
}
fclose(source);
Right now, I'm just trying to print to the screen the data that is in the file.
Eventually, I wanted to then connect to a database and run queries so that I could loop thru and load this data from the file into the appropriate table.
Can you look thru and see what I'm doing wrong.
Thanks in Advance
John
Here's my file definition and my main():
--------------------------------------------
struct record {
char bnumber[ 20 ];
char last_act_date[ 16 ];
char last_act_user[ 12 ];
char school_yr[ 4 ];
char work_level[ 1 ];
char agency[ 5 ];
char program[ 3 ];
char bill [1];
char fund_type[ 3 ];
char fund[ 3 ];
char account[ 2 ];
char fed_yr[ 4 ];
char object[ 1 ];
char sub_object[ 1 ];
char stat_sub_obj[ 3 ];
char priority[ 1 ];
char status[ 1 ];
char neg_code[ 3 ];
char transaction[ 5 ];
char req_type[ 1 ];
char rec_amt[ 20 ];
char cash_impact[ 20 ];
char desc[ 80 ];
char approp_rec_num[ 20 ];
char cat[ 2 ];
char order[ 4 ];
char type[ 1 ];
char workforce[ 12 ];
};
int main()
{
FILE *source;
int n_bytes = 0;
struct record cuts;
source = fopen("CUTS_2002.TXT","r"
if(!source)
{
printf("no data file found\n"
exit(0);
8 }
while (0 != (n_bytes = fread(&cuts, 1, sizeof(struct record), source)) )
{
printf("Bytes Read: %d\n", n_bytes);
printf("%s %s %s %s %s\n",
cuts.bnumber,
cuts.agency,
cuts.program,
cuts.bill,
cuts.fund);
fread(&cuts, 1, sizeof(struct record), source);
}
fclose(source);