Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to read numbers from two files using fscanf function and write it in another file using fprintf function in ascending order

Posted By: Daniel Evans     Category: C Programming     Views: 4245

Write a program to read numbers from two files using fscanf function and write it in another file using fprintf function in ascending order.

Code for Program to read numbers from two files using fscanf function and write it in another file using fprintf function in ascending order in C Programming

# include<stdio.h>
# include<conio.h>
void main(int argc, char *argv[])
{
    FILE *ptf1,*ptf2,*ptf3;
    int a,b;
    clrscr();
    a=b=0;

    ptf1=fopen(argv[1],"r");
    ptf2=fopen(argv[2],"r");
    ptf3=fopen(argv[3],"w");

    fscanf(ptf1,"%d",&a);
    fscanf(ptf2,"%d",&b);

    while(1)
    {
        if(b==0 || a==0)
            break;
        if(a<b)
        {
            fprintf(ptf3,"%d ",a);
            fscanf(ptf1,"%2d",&a);
        }
        if(a>b)
        {
            fprintf(ptf3,"%d ",b);
            fscanf(ptf2,"%2d",&b);
        }
        if(a==b)
        {
            fprintf(ptf3,"%d ",a);
            fscanf(ptf1,"%2d",&a);
            fscanf(ptf2,"%2d",&b);
        }
    }
    if(a==0 & b!=0)
    {
        while(1)
        {
            fprintf(ptf3,"%d ",b);
            //putw(b,ptf3);//b=getw(ptf2);
            fscanf(ptf2,"%2d",&b);
            if(b==0)
                break;
        }
    }
    if(a!=0 & b==0)
    {
        while(1)
        {
            fprintf(ptf3,"%d ",a);
            fscanf(ptf1,"%2d",&a);
            if(a==0)
                break;
        }
    }
    fclose(ptf1);
    fclose(ptf2);
    fclose(ptf3);
}
  
Share: 



Daniel Evans
Daniel Evans author of Program to read numbers from two files using fscanf function and write it in another file using fprintf function in ascending order is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!