Hi,
I need to write a program that opens two files. The program prints line 1 of the first file, line 1 of the second file, line 2 of the first file, line 2 of the second file, and so on, until the last line of the longer file is printed.
no what I have is:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *first, *second;
char file1[MAX], file2[MAX];
puts("Enter the name first file to be processed:");
gets(file1);
puts("Enter the name of the second file to be processed:");
gets(file2);
if((first = fopen(file1, "r")) == NULL)
{
printf("Can't open %s\n", file1);
exit(1);
}
if((second = fopen(file2, "r")) == NULL)
{
fprintf(stdout, "Can't open file %s.\n", file2);
exit(1);
}
puts("File contents: ");
while(fscanf(first, "%s", file1) == 1)
{
puts(file1);
if(fscanf(second, "%s", file2) == 1)
{
puts(file2);
}
}
fclose(first);
fclose(second);
return 0;
}
Now what I have works only if file1 is longer then file 2. I have also attempted, without success:
Code:
while(fscanf(first, "%s", file1) == 1 && fscanf(second, "%s", file2) == 1)
{
puts(file1);
puts(file2);
}