Please post the solution and the output. Please save the program with the name \
ID: 3860754 • Letter: P
Question
Please post the solution and the output. Please save the program with the name 'files.c'. Write a program that merges two files as follows.
One file will contain usernames (usernames.txt):
Moodie
Intelllligent
Happee ....
The other file will contain passwords (passwords.txt):
1 2 3 4 5 6
password
1 2 3 4 5 6 7 8 ...
The program should create a third file matching username and passwords (usernamesPasswords.txt):
Moodie 1 2 3 4 5 6
Intelllligent password
Happee ... 1 2 3 4 5 6 7 8 ...
Give the user of your programs the option of displaying your output file.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define LINE_SIZE 80
int main()
{
FILE *file1, *file2, *file3;
char userName[LINE_SIZE]={0,} ;
char password[LINE_SIZE]={0,};
file1 = fopen("username.txt", "r");
if(file1==NULL)
{
printf("username.txt file is not present. ");
return 1;
}
file2 = fopen("password.txt", "r");
if(file2==NULL)
{
printf("password.txt file is not present.. ");
return 1;
}
file3 = fopen("usernamesPasswords.txt", "w");
if(file3==NULL)
{
printf("Not able to open usernamesPasswords.txt. ");
return 1;
}
while(( fgets(userName, LINE_SIZE, file1) != NULL) && (fgets(password, LINE_SIZE, file2) != NULL))
{
int i=0;
while(userName[i]!=NULL)
{
if(userName[i]==' ')
{
userName[i]=0;
}
i++;
}
fprintf(file3, "%10s %s",userName,password);
memset(userName,0 ,sizeof(userName));
memset(password,0,sizeof(password));
}
// closing file...
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
output file:
Moodie 1 2 3 4 5 6
Intelllligent password
Happee ... 1 2 3 4 5 6 7 8 ...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.