Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> //Function prototypes void displayFile();

ID: 3916220 • Letter: #

Question

#include <stdio.h>

#include <stdlib.h>

//Function prototypes

void displayFile();

//Main function

int main()

{

char userName[30], password[30];

char ch;

//Two file pointers

FILE *file1, *file2, *outFile;

//Opening files in read mode

file1 = fopen("usernames.txt", "r");

file2 = fopen("passwords.txt", "r");

//Opening file in write mode

outFile = fopen("usernamesPasswords.txt", "w");

//Checking for file opening status

if(file1==NULL || file2==NULL)

{

printf(" Sorry!!! Unable to open files.... ");

return -1;

}

//Reading data from file line by line

while( (fscanf(file1, "%s", userName) == 1) && (fscanf(file2, "%s", password) == 1) )

{

//Writing to new file

fprintf(outFile, "%s %s ", userName, password);

}

//Closing files

fclose(file1);

fclose(file2);

fclose(outFile);

printf(" Files Merged Successfully.... ");

//Displaying choice

printf(" Do you want to display output file? (y/n): ");

scanf("%c", &ch);

//Checking user option

if(ch == 'y' || ch == 'Y')

{

//Calling function

displayFile();

}

printf(" ");

return 0;

}

//Function that displays file content

void displayFile()

{

char line[60];

FILE *fileReader;

//Opening file in read mode

fileReader = fopen("usernamesPasswords.txt", "r");

//Checking for file opening status

if(fileReader==NULL)

{

printf(" Sorry!!! Unable to open file.... ");

return -1;

}

//Reading data from file line by line

while( fgets(line, sizeof(line), fileReader) )

{

//Writing to new file

printf("%s ", line);

}

//Closing file

fclose(fileReader);

}

CAN ANYONE CORRECT THIS PROGRAM

Explanation / Answer

To correct the program use puts(line) instead of printf("%s ", line).

The complete program is given as:

#include <stdio.h>

#include <stdlib.h>

//Function prototypes

void displayFile();

//Main function

int main()
{
char userName[30], password[30];
char ch;
//Two file pointers
FILE *file1, *file2, *outFile;
//Opening files in read mode
file1 = fopen("usernames.txt", "r");
file2 = fopen("passwords.txt", "r");
//Opening file in write mode
outFile = fopen("usernamesPasswords.txt", "w");
//Checking for file opening status
if(file1==NULL || file2==NULL)
{
printf(" Sorry!!! Unable to open files.... ");
return -1;
}

//Reading data from file line by line
while( (fscanf(file1, "%s", userName) == 1) && (fscanf(file2, "%s", password) == 1) )
{
//Writing to new file
fprintf(outFile, "%s %s ", userName, password);
}

//Closing files
fclose(file1);
fclose(file2);
fclose(outFile);
printf(" Files Merged Successfully.... ");
//Displaying choice
printf(" Do you want to display output file? (y/n): ");
scanf("%c", &ch);
//Checking user option
if(ch == 'y' || ch == 'Y')
{
//Calling function
displayFile();
}
printf(" ");
return 0;
}

//Function that displays file content
void displayFile()
{
char line[60];
FILE *fileReader;
//Opening file in read mode
fileReader = fopen("usernamesPasswords.txt", "r");
//Checking for file opening status
if(fileReader==NULL)
{
printf(" Sorry!!! Unable to open file.... ");
return -1;
}

//Reading data from file line by line
while( fgets(line, sizeof(line), fileReader) )
{
//Writing to new file
puts( line);
}
//Closing file
fclose(fileReader);
}