PROBLEM WITH FILE HANDLING IN C

hello i am new to c and practice on file handling in c....
and program of how to merging two txt file into one txt file side by side facing issue.....
there are 3 files RFID.txt,FP.txt and USERID.txt

1)In RFID.txt
ae 34 er 45

sd gg 5t t5

dd tu 66 ui

t5 y5 t5 u8

f5 h7 hh y6

2)In FP.txt
1

2

3

4

5

3)and in merging file USERID.txt
ae 34 er 45

sd gg 5t t5

dd tu 66 ui

t5 y5 t5 u8

f5 h7 hh y6

1

2

3

4

5

above is the output of program but i want following output==>

ae 34 er 45 1

sd gg 5t t5 2

dd tu 66 ui 3

t5 y5 t5 u8 4

f5 h7 hh y6 5

plz tell me how to do it?
my main.c file is ==>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1,*fp2,*fp3;
char ch=0;
if((fp1=fopen("RFID.txt","r")) == NULL)
{
printf("error in opening file\n");
exit(1);
}
if((fp2=fopen("FP.txt","r")) == NULL)
{
printf("error in opening file\n");
exit(1);
}
if((fp3=fopen("USERID.txt","w")) == NULL)
{
printf("error in opening file\n");
exit(1);
}

printf("merged RFID.txt and FP.txt into USERID.txt");
while((ch=fgetc(fp1)) != EOF)
{
fputc(ch,fp3);
}
while((ch=fgetc(fp2)) != EOF)
{
fputc(ch,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
You need to read the files RFID.txt and FP.txt line by line with fgets and write the line from RFID.txt and then the line from FP.txt.
@Thomas1965 thanks for your reply...."write the line from RFID.txt and then the line from FP.txt"....cant get it. actually i have make the code of reading line by line from these two files RFID.txt and FP.txt....

#include<stdio.h>
int main()
{
FILE *fa,*fb,*fc;
fa=fopen("RFID.txt","r");
fb=fopen("FP.txt","r");
fc=fopen("USERID.txt","w");
char str1[256];
char ch1,ch2,c;
while(((ch1=fgetc(fa))!=EOF) && ((ch2=fgetc(fb))!=EOF))
{

if(ch1 != EOF) //getting lines in alternately from two files
{
ungetc(ch1,fa);
fgets(str1, 255,fa);
fputs(str1,fc);
}
if(ch2 != EOF)
{
ungetc(ch2,fb);
fgets(str1, 255,fb);
fputs(str1,fc);
}

}
fclose(fa);
fclose(fb);
fclose(fc);
}

the program output is same==> ae r4 tt rf
1
but i want the output as ==> ae r4 tt rf 1
Last edited on
The fgets() leaves the newline at the end of the string. After reading each line from RFID.txt, use strchr() to find the position of that newline and then replace that character with a space.
@Chervil i have make changes as below but same output ==>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1,*fp2,*fp3;
char str1[60];
char str2[60];
char ch = ' ';
char *ret=0;
if((fp1=fopen("RFID.txt","r")) == NULL)
{
perror("error in opening file\n");
exit(1);
}
if((fp2=fopen("FP.txt","r")) == NULL)
{
perror("error in opening file\n");
exit(1);
}
if((fp3=fopen("USERID.txt","r+")) == NULL)
{
perror("error in opening file\n");
exit(1);
}

if( fgets (str1, 60, fp1)!=NULL )
{

fputs(str1,fp3); //writing content to stdout
}

if( fgets (str2, 60, fp2)!=NULL && str2 == '\n')
{

ch = getchar();
ret=strchr(str2,ch);
fputs(str2,fp3);
}

fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	
    while (((ch1=fgetc(fa))!=EOF) && ((ch2=fgetc(fb))!=EOF))
    {
        if (ch1 != EOF) //getting lines in alternately from two files
        {
            ungetc(ch1,fa);
            fgets(str1, 255,fa);
            
            /*
            After reading each line from RFID.txt, 
            use strchr() to find the position of the newline 
            and then replace that character with a space. 
            */
            char * nl = strchr(str1, '\n');
            if (nl)
            {
                *nl = ' ';    // replace newline with space
            }            

            fputs(str1,fc);
        }
        
        if (ch2 != EOF)
        {
            ungetc(ch2,fb);
            fgets(str1, 255,fb);
            fputs(str1,fc);
        }
    }
Last edited on
@Chervil...thanku for your help...actually i want to change in first string and i change in second...
Topic archived. No new replies allowed.