Originally Posted by
RicsterB
Say I have a text file called "accounts.txt" that contained the following:
The format is like this: (account number)?(username)?(password)
Now the following code is to search through the accounts.txt file (not complete):
Code:
void Login()
{
char unameInput[30], uname[30];
char pwdInput[30], pwd[30];
int accNumInput, accNum;
printf("\n\tLogin to see your Bank Account\n\n");
printf("\t Enter Account Username: ");
fgets(uname, 30, stdin);
printf("\n\t Enter Password: ");
fgets(pwd, 30, stdin);
printf("\n\t Enter Account Num (required): ");
scanf("%d", &accNum);
printf("\n");
/* Get rid of the newlines in the unameInput and pwdInput */
unameInputLen = strlen(unameInput);
pwdInputLen = strlen(pwdInput);
if(unameInput[unameInputLen-1] == '\n' && pwdInput[pwdInputLen-1] == '\n')
{
unameInput[unameInputLen-1] = '\0';
pwdInput[pwdInputLen-1] = '\0';
}
/* Assign the edited unameInput and pwdInput as well as accNumInput to their new variables */
accNum = accNumInput;
memcpy(uname, unameInput, sizeof(unameInput));
memcpy(pwd, pwdInput, sizeof(pwdInput));
/* Search the accounts.txt file to see if there the user information matches any of the accounts stored */
char search[30];
FILE *searchaccount = fopen("accounts.txt", "r");
while((accNum = getchar()) != EOF && accNum != '\n'); //clears the input buffer
}
The code is when a user enters their information, the code will then search through the file and if there's a match, append that information on to static variables to be displayed throughout the code.
But I'm stuck on this part:
Code:
/* Search the accounts.txt file to see if there the user information matches any of the accounts stored */
char search[30];
FILE *searchaccount = fopen("accounts.txt", "r");
Do I use fscanf() and strstr()? I'm confused?
Try to use functions whenever possible. Also, scanf and fgets don't play well together. Better to use sscanf instead.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* read_line(char* buffer, size_t size, FILE* stream)
{
char* result = fgets(buffer, size, stream);
char* newline = strstr(buffer, "\n");
if(newline)
*newline = 0;
return result;
}
typedef int BOOLEAN;
BOOLEAN read_int(int* value)
{
char tmp[32];
read_line(tmp, sizeof tmp, stdin);
int check = sscanf(tmp, "%d", value);
return check == 1;
}
#define MAX_LINE 30
int main()
{
char uname[MAX_LINE];
char pwd[MAX_LINE];
char buf[MAX_LINE];
int accNum;
printf("\n\tLogin to see your Bank Account\n\n");
printf("\t Enter Account Username: ");
read_line(uname, MAX_LINE, stdin);
printf("\n\t Enter Password: ");
read_line(pwd, MAX_LINE, stdin);
printf("\n\t Enter Account Num (required): ");
if(!read_int(&accNum))
{
puts("\n\t Error: invalid format for account number!");
return EXIT_FAILURE;
}
printf("\n");
printf("\n\t Username: %s\n", uname);
printf("\n\t Account Num: %d\n", accNum);
printf("\n");
printf("\n\tExisting Accounts:");
printf("\n");
char search[MAX_LINE];
FILE *searchaccount = fopen("accounts.txt", "r");
while(read_line(search, MAX_LINE, searchaccount))
{
printf("\n\t%s\n", search);
}
return EXIT_SUCCESS;
}