I am working on a C project, to which I have little
experience. I am starting with a simple program - read input, load into an array and then write out a new file.
The problem is either I'm not loading of the array properly or I'm not writing out to the file the right way. I say this because the printf during the loop doesn't look right and I get a coredump on the memcpy into the outFILE.
The Input has 4 records and looks like this:
1234567TestBid222100.50
7654321TestBid333288.75
6543210TestBid444523.80
5432109TestBid555825.75
The printf's are as follows:
In the init array....
The array position is 0.
The array position is 1.
The array position is 2.
The array position is 3.
There are 4 records in the array.
The array position is 0
The bid is : TestBid222
765432
11472 Memory fault(coredump)
experience. I am starting with a simple program - read input, load into an array and then write out a new file.
The problem is either I'm not loading of the array properly or I'm not writing out to the file the right way. I say this because the printf during the loop doesn't look right and I get a coredump on the memcpy into the outFILE.
The Input has 4 records and looks like this:
1234567TestBid222100.50
7654321TestBid333288.75
6543210TestBid444523.80
5432109TestBid555825.75
The printf's are as follows:
In the init array....
The array position is 0.
The array position is 1.
The array position is 2.
The array position is 3.
There are 4 records in the array.
The array position is 0
The bid is : TestBid222
765432
11472 Memory fault(coredump)
Code:
#include <stdio.h>
#include <stdlib.h>
#define TYPELEN 7
#define BIDLEN 10
#define AMTLEN 6
/*-- Pointers --*/
FILE *fpIN;
FILE *fpOUT;
int col;
int row;
int tot;
/* Input File */
typedef struct
{
char Type [TYPELEN];
char Bid [BIDLEN];
char Amt[AMTLEN];
}
tIN;
tIN inFILE;
/* Output File */
typedef struct
{
char Type [TYPELEN];
char Bid [BIDLEN];
}
tOUT;
tOUT outFILE;
/*-- Array --*/
struct tKEY
{
char KeyType[TYPELEN];
char KeyBid[BIDLEN];
};
struct tKEY cpsmKEY[4][2];
main()
{
/*Open stuff.....*/
/*-- Read and Load Input File into array. --*/
printf(" In the init array....\n");
while ( fread((char *) &inFILE,sizeof(inFILE),1,fpIN)==1)
{
memcpy(cpsmKEY[tot][0].KeyType,inFILE.Type,TYPELEN);
memcpy(cpsmKEY[tot][1].KeyBid,inFILE.Bid,BIDLEN);
printf("The array position is %d.\n", tot);
++tot;
}
printf("There are %d records in the array.\n", tot);
printf("\n");
/*-- Loop thru array and write out new file--*/
for (row = 0; row < tot; row++)
{
printf("The array position is %d\n", row);
printf("The bid is : %10s\n", cpsmKEY[row][1].KeyBid);
printf("\n");
memcpy(outFILE.Type,cpsmKEY[row][0].KeyType);
memcpy(outFILE.Bid,cpsmKEY[row][1].KeyBid);
fwrite(&outFILE,1,sizeof(outFILE), fpOUT);
}
}