Thread: string print from 2 files

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    string print from 2 files

    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);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Having processed lines from both files, until one or the other has reached EOF, you then do
    Code:
    while(fscanf(first, "%s", file1) == 1 ) puts(file1);
    while(fscanf(second, "%s", file2) == 1 ) puts(file2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    That does not print them in the order required.
    I have output1 which is:
    Code:
    1.1
    1.2
    1.3
    1.4
    1.5
    1.6
    1.7
    and output2 which is:
    Code:
    2.1
    2.2
    2.3
    2.4
    what I want it to print is:
    Code:
    ...
    2.3
    1.4
    2.4
    1.5
    1.6
    ...
    which works with the original code assuming output1 is longer then output2.
    what I got testing the suggestion was

    Code:
    1.1
    1.2
    1.3
    ...
    2.1
    2.2
    2.3
    2.4

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You mis-understood.

    You use the loop you have to read BOTH files one line at a time, until one or other of the files reaches EOF.

    THEN you use the code I posted to read the remaining lines (from whichever file hasn't reached EOF) to append those lines to the output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Files with strings and ints won't print to screen!
    By Kristyy_mariee in forum C Programming
    Replies: 16
    Last Post: 11-26-2011, 12:07 PM
  2. c program to print list of files in a directory
    By leo_1024 in forum Linux Programming
    Replies: 3
    Last Post: 01-24-2009, 05:54 AM
  3. Quick question, print to two files
    By vutek0328 in forum Tech Board
    Replies: 4
    Last Post: 07-18-2007, 03:55 PM
  4. print Random Files
    By swishiat.com in forum C Programming
    Replies: 4
    Last Post: 12-15-2003, 01:58 PM
  5. How do I match 2 files to print data.
    By sketchit in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 05:45 PM