P
pillip
I am trying to use fopen and fget to input two files and then output them
into one file. Each input file has two columns and 20 rows, however since
the first column in each input file is same ( numbers 0...19), i want the
output file to have 3 columns and 20 rows. Right now i am using a one
dimensional array "array1[SIZE]" to input each file:, and the output file
has 2 columns and 42 rows.
#include <stdlib.h>
#include <stdio.h>
#define SIZE 20
main()
{
char array1[SIZE], array2[SIZE];
FILE *fp;
if ( (fp = fopen("1a.data", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array1, SIZE, fp);
printf("%s",array1);
}
fclose(fp);
printf("\n\n");
if ( (fp = fopen("1b.data", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array2, SIZE, fp);
printf("%s",array2);
}
fclose(fp);
return(0);
}
into one file. Each input file has two columns and 20 rows, however since
the first column in each input file is same ( numbers 0...19), i want the
output file to have 3 columns and 20 rows. Right now i am using a one
dimensional array "array1[SIZE]" to input each file:, and the output file
has 2 columns and 42 rows.
#include <stdlib.h>
#include <stdio.h>
#define SIZE 20
main()
{
char array1[SIZE], array2[SIZE];
FILE *fp;
if ( (fp = fopen("1a.data", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array1, SIZE, fp);
printf("%s",array1);
}
fclose(fp);
printf("\n\n");
if ( (fp = fopen("1b.data", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array2, SIZE, fp);
printf("%s",array2);
}
fclose(fp);
return(0);
}