please check the error and correct the program
3 posters
Page 1 of 1
Re: please check the error and correct the program
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);
}
#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);
}
Re: please check the error and correct the program
it is not giving the correct answer.please code,compile,run and check..
akarshsomani- Posts : 21
Join date : 2017-04-04
This Program is almost working fine
//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;
}
//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;
}
iamdsc_0410- Posts : 14
Join date : 2017-04-11
Age : 26
Location : IIIT Kalyani
you have to save the alternate lines in the third file instead of direct printing.
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
Re: please check the error and correct the program
Read the question again!
But storing in a third file can also be done just that will make the code longer and complex
But storing in a third file can also be done just that will make the code longer and complex
iamdsc_0410- Posts : 14
Join date : 2017-04-11
Age : 26
Location : IIIT Kalyani
Re: please check the error and correct the program
check my code(first comment in this question).please tell me the error.
akarshsomani- Posts : 21
Join date : 2017-04-04
Re: please check the error and correct the program
We will discuss the errors later when we meet !!
iamdsc_0410- Posts : 14
Join date : 2017-04-11
Age : 26
Location : IIIT Kalyani
Similar topics
» in ipl question.the test case 2nd is wrong.my solution is correct.
» templet program
» Overloading new and delete operator program
» IP6: Write a C++ program that uses the concept of hybrid inheritance.
» Program related to Object Slicing : Why the output is not coming In Base a=2 ?
» templet program
» Overloading new and delete operator program
» IP6: Write a C++ program that uses the concept of hybrid inheritance.
» Program related to Object Slicing : Why the output is not coming In Base a=2 ?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|