Reputation: 9
I'm new to C and trying to write a program for a hotel and let customers edit their booking details such as first name, last name...etc. The code I wrote can be run but the data in the file is not edited.
The else statement for the line ("Record not found! Please enter your Username and Password again.\n");
is not printed when I entered the wrong username and password as well.
Here is what I got so far:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct customer_details // Structure declaration
{
char uname[100];
char pass[100];
char fname[100];
char lname[100];
char ic[100];
char contact[100];
char email[100];
char room[100];
char day[100];
}s;
void main()
{
FILE* f, *f1;
f = fopen("update.txt", "r");
f1 = fopen("temp.txt", "w");
char uname[100], pass[100];
int valid = 0;
// validate whether file can be opened.
if (f == NULL)
{
printf("Error opening file!\n");
exit(0);
}
printf("Please enter your Username and Password to update your booking detail.\n");
printf("\nUsername: ");
fgets(uname, 100, stdin);
uname[strcspn(uname, "\n")] = 0;
printf("Password: ");
fgets(pass, 100, stdin);
pass[strcspn(pass, "\n")] = 0;
while (fscanf(f, "%s %s", s.uname, s.pass) != -1)
{
if (strcmp(uname, s.uname) == 0 && strcmp(pass, s.pass) == 0)
{
valid = 1;
fflush(stdin);
printf("Record found!\n");
printf("\nEnter First Name: ");
scanf("%s", &s.fname);
printf("\nEnter Last Name: ");
scanf("%s", &s.lname);
printf("\nEnter IC Number: ");
scanf("%s", &s.ic);
printf("\nEnter Contact Number:");
scanf("%s", &s.contact);
printf("\nEnter Email: ");
scanf("%s", &s.email);
printf("\nEnter Room ID :");
scanf("%s", &s.room);
printf("\nEnter Days of staying:");
scanf("%s", &s.day);
fseek(f, sizeof(s), SEEK_CUR); //to go to desired position infile
fwrite(&s, sizeof(s), 1, f1);
}
else
("Record not found! Please enter your Username and Password again.\n");
}
fclose(f);
fclose(f1);
if (valid == 1)
{
f = fopen("update.txt", "r");
f1 = fopen("temp.txt", "w");
while (fread(&s, sizeof(s), 1, f1))
{
fwrite(&s, sizeof(s), 1, f);
}
fclose(f);
fclose(f1);
printf("Record successfully updated!\n");
}
}
This is what the update.txt file contains:
abc 123 First Last 234 33667 [email protected] 101 3
Upvotes: 0
Views: 105
Reputation: 11377
You forgot the printf call, change
("Record not found! Please enter your Username and Password again.\n");
to
printf("Record not found! Please enter your Username and Password again.\n");
If you compile your program with warnings enabled you should get a warning like
t.c:68:12: warning: statement with no effect [-Wunused-value]
("Record not found! Please enter your Username and Password again.\n");
^
Upvotes: 2