please check the error and correct the program

View previous topic View next topic Go down

Re: please check the error and correct the program

Post by Prateek Gupta on Thu Apr 06, 2017 11:37 pm

I have just kept your getc() functions in the loop condition so that it continuously takes input and not once as it was taking previously in your given program

#include <stdio.h>
void main()
{
FILE *fp,*fp1,*fp2;

fp=fopen("F://emp.txt","w");
fp1=fopen("F://a.txt","r");
fp2=fopen("F://a1.txt","r");
char ch1,ch2;

while((ch1=getc(fp1))!=EOF&&(ch2=getc(fp2))!=EOF)
{
while(ch1!='\n'&&ch1!=EOF)
{
putc(ch1,fp);
ch1=getc(fp1);
}
putc('\n',fp);
while(ch2!='\n'&&ch2!=EOF)
{
putc(ch2,fp);
ch2=getc(fp2);
}
putc('\n',fp);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
}

Prateek Gupta
Admin

Posts : 6
Join date : 2017-04-04

View user profile http://clanofcoders.forumotion.com

Back to top Go down

Re: please check the error and correct the program

Post by akarshsomani on Fri Apr 07, 2017 12:31 pm

it is not giving the correct answer.please code,compile,run and check..

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

This Program is almost working fine

Post by iamdsc_0410 on Tue Apr 11, 2017 7:07 pm

//merging lines of two files
//make two files named emp.doc and emp1.doc
#include<stdio.h>
int main()
{
FILE *fp,*fp1;
char c,c1;
int i=0,j=0;
while((c!=EOF)||(c1!=EOF))
{
c='\0';
fp=fopen("emp.doc","r");
fseek(fp,i,SEEK_SET);
while(c!='\n'&&c!=EOF)
{
c=getc(fp);
i++;
putchar(c);
}
fclose(fp);
c1='\0';
fp1=fopen("emp1.doc","r");
fseek(fp,j,SEEK_SET);
while(c1!='\n'&&c1!=EOF)
{
c1=getc(fp1);
j++;
putchar(c1);
}
fclose(fp1);
}
return 0;
}
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 19
Location : IIIT Kalyani

View user profile

Back to top Go down

you have to save the alternate lines in the third file instead of direct printing.

Post by akarshsomani on Tue Apr 11, 2017 10:07 pm

iamdsc_0410 wrote://merging lines of two files
//make two files named emp.doc and emp1.doc
#include<stdio.h>
int main()
{
FILE *fp,*fp1;
char c,c1;
int i=0,j=0;
while((c!=EOF)||(c1!=EOF))
{
c='\0';
fp=fopen("emp.doc","r");
fseek(fp,i,SEEK_SET);
while(c!='\n'&&c!=EOF)
{
c=getc(fp);
i++;
putchar(c);
}
fclose(fp);
c1='\0';
fp1=fopen("emp1.doc","r");
fseek(fp,j,SEEK_SET);
while(c1!='\n'&&c1!=EOF)
{
c1=getc(fp1);
j++;
putchar(c1);
}
fclose(fp1);
}
return 0;
}

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: please check the error and correct the program

Post by iamdsc_0410 on Tue Apr 11, 2017 10:12 pm

Read the question again!
But storing in a third file can also be done just that will make the code longer and complex
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 19
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: please check the error and correct the program

Post by akarshsomani on Tue Apr 11, 2017 10:23 pm

check my code(first comment in this question).please tell me the error.

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: please check the error and correct the program

Post by iamdsc_0410 on Tue Apr 11, 2017 10:33 pm

We will discuss the errors later when we meet !!
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 19
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: please check the error and correct the program

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum