#include<iostream.h> #include<fstream.h> #include<string.h> #include<conio.h> #i
ID: 3915673 • Letter: #
Question
#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. ");
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);
}
char choice,ch;
printf("File Username Merged With File Password... ");
printf("Do You Want to Display File Contents ");
scanf("%c",&choice);
fseek(file3, 0, SEEK_SET); // Set the file pointer to start of file
printf("The Contents of usernamePasswords file is ");
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 ");
}
fclose(file);
fclose(file2);
fclose(file3);
return 0;
}
}
Can anyone correct this program i cannt run it
Explanation / Answer
UPDATED PROGRAM
username.txt file contents
abcd124
xyz394
admin
lmnop899
password.txt file contents
xxx1234
yy9888
zz1234
ppppx344
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <iomanip>
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 */
// set file path i am create file in f: location
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
abcd124 xxx1234
xyz394 yy9888
admin zz1234
lmnop899 ppppx344
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.