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

I\'ve been racking my brain over this for a while. For some reason when i fprint

ID: 3625081 • Letter: I

Question

I've been racking my brain over this for a while. For some reason when i fprintf the outputs don't line up. I Suspect it my method of using fgets. I would greatly appreciate a solution.

This program must be written using c language.

Write a program that merges two files as follows.

One file will contain user names:
foster001
smith023
nguyen002
. . .

The other file will contain passwords:
x34rdf3e
p43e4rdd
w32eds22
. . .

The program should create a third file matching usernames and passwords:
foster001 x34rdf3e
smith023 p43e4rdd
nguyen002 w32eds22
. . .

Give the user of your program the option of displaying your input file.

The following criteria have been added to the problem.

1) Add a third input file that hold the balance (float). The final file should now contain the username, password and balance.
2) Answers to be displayed on the screen as well as going to the file.

Explanation / Answer

I think it's the new lines in the usernames and passwords files that get copied along with the fgets function:
FILE *usersFile, *passFile, *combinedFile;
char* user, pass;
//open them for read/write/append and then get strings per line here:
while ( fgets(user,20,usersFile)!=NULL && fgets(pass,20,passFile)!=NULL)
{
//assuming our user names and passwords don't exceed 20 characters here,
// we place a '' character at the end to terminate the string and get rid of the
// of course, we assume that the last line in your file is also a to a blank line
//otherwise we lose the last character of the last line in user/pass files
// so add a new line at the end of your files after the last username/password
user[strlen(user)-1]='';
pass[strlen(pass)-1]='';
fprintf(combinedFile,"%s %s ", user, pass);
}
//close your files for reading and writing/appending here

==================================================================
I think the balance addition can be done using
fscanf(balanceFile,%f,&balance) for each cycle through the loop
and we change the printing to be:
fprintf(combinedFile,"%s %s %f",user,pass,balance);

==========================================================================
For displaying to the user, we ask before doing anything if the user wants to see what's going on : Y/N
if(ans=='Y') { another printf exactly identical to the fprintf added next to it }

like so:

while(...)

{

     user[strlen(user)-1]='';
     pass[strlen(pass)-1]='';
    fprintf(combinedFile,"%s %s %f ", user, pass, balance);

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

         printf("%s %s %f ", user, pass, balance);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote