In: Electrical Engineering
Edit 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):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
C Program:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <iomanip.h>
using namespace std;
char *substring(size_t start, size_t finish, const char *src, char
*merge, size_t size)
{
int count = finish - start;
if ( count >= --size )
{
count = size;
}
sprintf(merge, "%.*s", count, src + start);
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("username.txt", "r");
file2 = fopen("password.txt", "r");
file3 = fopen("usernamesPasswords.txt", "w");
/* open a text file for reading */
if(file==NULL) {
printf("Error: can't open username file.\n");
return 1;
}
else if(file2==NULL) {
printf("Error: can't open password file.\n");
return 1;
}
else if(file3==NULL) {
printf("Error: can't open usernamesPasswords file.\n");
return 1;
}
else {
char * p;
while(fgets(c2, 200, file2) != NULL && fgets(c, 200, file)
!= NULL) {
/* keep looping until NULL pointer... */
p = strchr(c, '\n');
if (p)
{
*p = '\0';
}
p = strchr(c2, '\n');
if (p)
{
*p = '\0';
}
fprintf(file3, "%s %s\n",c, c2);
}
char choice,ch;
printf("File Username Merged With File Password...\n");
printf("Do You Want to Display File Contents\n");
scanf("%c",&choice);
fseek(file3, 0, SEEK_SET); // Set the file pointer to start of
file
printf("The Contents of usernamePasswords file is\n");
if (choice == 'Y' || choice == 'y')
{
file3 = fopen("usernamesPasswords.txt", "r");
while(1)
{
ch = fgetc(file3);
if( feof(file3) )
{
break;
}
printf("%c", ch);
}
}
else
{
printf("Program Closing\n");
}
fclose(file);
fclose(file2);
fclose(file3);
return 0;
}
}