I recommend you do make three "Random Access Files" (because they are more organized), but first:
- You need to format each file with the structure It will use, in the first file the structure [1] You just need one member the name (char array), in the second structure [2] you need three members the name (char array) and 2 values (I suppose two integers), and the third stricture is the same as the second so you can re-utilize it.
[1]
/* Struct for the first file. */
typedef {
char name[4];
} file1_t;
[2]
/* Struct for the second file. */
typedef {
char name[4];
int value1, value2;
} file2_t;
To create those files you will have to use a code like this (Supposing that the file is opened in "wb" mode, but not created):
/* This is an incomplete example (I can't make your whole Homework)*/
void file2Creator( FILE *fPtr )
{
int i; // Counter to create the file.
file2_t data = { "", 0, 0 }; // A blank example to format the file.
/* You will create 9 consecutive records*/
for( i = 1; i <= 9; i++ ){
fwrite( &data, sizeof( file2_t ), 1, fPtr );
}
fclose( fPtr ); // You can close the file here or later however you need.
}
After that you have to fill the blank spaces you made (I suppose the file is opened):
void fillFile2( FILE *fPtr)
{
int position;
file2_t data = { "", 0, 0 };
printf( "Enter the position to fill (1-9) 0 to finish:\n?" );
scanf( "%d", &position );
while( position != 0 ){
printf( "Enter the name, and the two other values (integers):\n?" );
fscanf( stdin, "%s%d%d", data.name, data.value1, data.value2 );
/* You have to seek the pointer. */
fseek( fPtr, ( position - 1 ) * sizeof( file2_t ), SEEK_SET );
fwrite( &data, sizeof( file2_t ), 1, fPtr );
printf( "Enter a new position (1-9) 0 to finish:\n?" );
scanf( "%d", &position );
}
fclose( fPtr ); //You can close the file or not, depends in what you need.
}
Now i imagine that you have the file1, file 2 and file 3 formatted, and the file 1 and 2 are filled, and you want to fill the file 3 how to do it? Simple.
/* 3 pointers to each file */
void fillFile3( FILE *f1Ptr, FILE *f2Ptr, FILE *f3Ptr )
{
int i, j, k, number;
char word[ 4 ];
file1_t data1 = { "" };
file2_t data2, data3 = { "", 0, 0 };
k = 1;
/* I suppose that files are opened with their correctly FILE pointers. */
for( i = 1; i <= 5; i++ ){
/* I locate in the "i" position of the file1. */
fseek( f1Ptr, ( i - 1 ) * sizeof( file1_t ), SEEK_SET );
/* I read the slot. */
fread( &data1, sizeof( file1_t ), 1, f1Ptr );
/*Now we compare the file2 until we find the correct value, if it is organized we can jump some slots if not compare with all.*/
for( j = 1; j <= 9, j++ ){
fseek( f2Ptr, ( j - 1 ) * sizeof( file2_t ), SEEK_SET );
fread( &data2, sizeof( file2_t ), 1, f2Ptr );
/* We compare the strings, If they are equal we paste the value in file 3*/
if( strcmp( data1.name, data2.name ) == 0 ){
/*file 3 is of type of the file 2*/
fseek( f3Ptr, ( k - 1 ) * sizeof( file2_t ), SEEK_SET );
fwrite( &data2, sizeof( file2_t ), 1, f3Ptr );
k++;
break;
}
}
}
fclose( f1Ptr );
fclose( f2Ptr );
fclose( f3Ptr );
}