Write a program that merges two files as follows. The two files are in the docsh
ID: 3915677 • Letter: W
Question
Write a program that merges two files
as follows. The two files are in the
docsharing which you can download it.
One file will contain usernames
(usernames.txt)
:
foster001
smith023
nyuyen002
...
The other file will contain passwords
(passwords.txt)
:
x34rdf3e
p43e4rdd
w32eds22
...
The program
should create a third file matching username and passwords
(usernamesPasswords.txt)
:
foster001
x34rdf3e
smith023
p43e4rdd
nyuyen002
w32eds22
...
...
Give the user of your programs the option of displaying you output file
CAN ANYONE SOLVE THIS PROBLEM IN C LANGUAGE
Explanation / Answer
PROGRAM
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
// implements substring() function
char *substring(size_t start, size_t finish, const char *src, char *merge, size_t size)
{
int count = finish - start;
if ( count >= --size ) // check condition count>size
{
count = size;
}
sprintf(merge, "%.*s", count, src + start);
return merge; // return merge
}
int main() {
char c[20] = " "; /* declare a char array */
char c2[20]= " "; /* declare a char array */
FILE *file, *file2, *file3; /* declare a FILE pointers */
file = fopen("F:\username.txt", "r");
file2 = fopen("F:\password.txt", "r");
file3 = fopen("F:\usernamesPasswords.txt", "w");
/* open a text file for reading */
if(file==NULL) {
printf("Error: can't open username file. ");
return 1;
}
else if(file2==NULL) {
printf("Error: can't open password file. ");
return 1;
}
else if(file3==NULL) {
printf("Error: can't open usernamesPasswords file. ");
return 1;
}
else {
char * p;
while(fgets(c2, 200, file2) != NULL && fgets(c, 200, file) != NULL) {
/* keep looping until NULL pointer... */
p = strchr(c, ' ');
if (p)
{
*p = '';
}
p = strchr(c2, ' ');
if (p)
{
*p = '';
}
fprintf(file3, "%s %s ",c, c2);
}
// close all files
fclose(file);
fclose(file2);
fclose(file3);
char choice,ch;
printf("File Username Merged With File Password... ");
printf("Do You Want to Display File Contents(Y/N): ");
scanf("%c",&choice);
printf("The Contents of usernamePasswords file is ");
if (choice == 'Y' || choice == 'y')
{
fseek(file3, 0, SEEK_SET); // Set the file pointer to start of file
file3 = fopen("f:\usernamesPasswords.txt", "r");
printf("UserNames Passwords ");
while((ch=getc(file3))!=EOF) // create while loop until end of file
{
printf("%c", ch);
}
}
else
{
printf("Program Closing ");
}
fclose(file3); // close file3
return 0;
}
}
OUTPUT
File Username Merged With File Password...
Do You Want to Display File Contents(Y/N): y
The Contents of usernamePasswords file is
UserNames Passwords
foster001 x34rdf3e
smith023 p43e4rdd
nyuyen002 w32eds22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.