0

I am new to C language and I need help.
The Program will ask the user the account number and Account Pin. Then will search the File "Account.txt". If the Account Number and Pin are matched to what was written on the file then will display another Option, such as Balance inquiry,Deposit, withdrawal and Quit.
How can it be possible to search the file for a match? What exactly will I do?

Here's some code:

void OpenAnAccount() {
  FILE * fptr;
  fptr = fopen("Account.txt", "w");

  if (fptr == NULL) {
    printf("File does not exists \n");
  }

  printf("*************************************\n\n");
  printf("\n ACCOUNT CREATION ");

  printf("\n**********************************\n");
  printf("Enter Your Account Number :\n");
  scanf("%d", & acctno);
  printf("Enter your Name:\n");
  scanf("%s", & acctname);
  printf("Enter your Account Pin: \n");
  scanf("%d", & acctpin);
  printf("Please Enter Your Initial Deposit :");
  scanf("%f", & dep);
  fprintf(fptr, "%d%s%d%f\n", acctno, acctname,
    acctpin);
}

void BankTransactions() {
  int AN, AP;
  printf("Enter 5 digits Account Number :\n");
  scanf("%d", & AN);
  printf("Enter 4 digits Accoount Pin :\n");
  scanf("%d", & AP);
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
AJ AJ
  • 27
  • 3
  • Please detail what you have tried and why it didn't work. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for more information. This might help: https://stackoverflow.com/q/3463426/7470360 – A Jar of Clay Mar 20 '19 at 09:07
  • My codes up there is working My problem is I dont know how to search and Match the account Number and Pin on the File that I created. Inside the file are the Account pin and number. – AJ AJ Mar 20 '19 at 09:36
  • 1
    can you give us some info on how your Account.txt file is formatted? That will make it much easier to find a solution – Pierre Mar 20 '19 at 09:37
  • After creating account which have account number , pin and name written inside the file i created, I am now entering by account number and pin in order to do transaction i need to make sure that the pin and number that were entered are matched. – AJ AJ Mar 20 '19 at 09:39
  • OT: regarding: `printf("File does not exists \n");` 1) Error messages should be output to `stderr`, not `stdout` 2) When the error is from a C library function, then should also output the text reason the system thinks the error occurred. The simplest way to perform both of the above is to call `perrorf("fopen failed");` – user3629249 Mar 21 '19 at 17:09
  • OT: regarding: `scanf("%s", & acctname);` and similar calls to `scanf()` 1) always check the returned value (not the parameter values) to assure the operation was successful (see the MAN page for `scanf()`) 2) when using '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer which allows for the NUL byte that those input specifiers always append to the input. This also avoids any buffer overflow and the resulting undefined behavior – user3629249 Mar 21 '19 at 17:14
  • the posted code does not compile!. It is missing the needed `#include` for the header file: `stdio.h` and is missing the declarations for the variables: `dep` and `acctpin` and `acctname` and to meet the stackoverflow requirements needs the structure of the data in the file and a `main()` function to be calling all the posted functions – user3629249 Mar 21 '19 at 17:20
  • regarding: `fprintf(fptr, "%d%s%d%f\n", acctno, acctname, acctpin);` This hints the file contains ascii data, where the length of each field is 'just' long enough to hold the current value. This will make the data 'difficult' to retrieve from the file. Suggest using a struct of all the fields so every field has a fixed length. Note: the format string expects 4 fields but the parameters only lists 3 fields, so this will not correctly compile – user3629249 Mar 21 '19 at 17:24
  • regarding: `if (fptr == NULL) { printf("File does not exists \n"); }` When the program fails to open the file, then it should NEVER try to access the file for reading/writing. I.E. after the call `printf()` should be `exit( EXIT_FAILURE );` And again that `prinft()` should be: `perror()` – user3629249 Mar 21 '19 at 17:30
  • Strongly suggest using a `struct` to layout the file. Similar to: `struct account { long acctNum; char acctName[30]; unsigned short acctPin; float acctDep };' – user3629249 Mar 21 '19 at 17:35
  • OT: regarding: `printf("Enter Your Account Number :\n"); scanf("%d", & acctno);` How is the user of this program to know the account number? Suggest tracking the highest used account number, and add 1 to get the new account number. – user3629249 Mar 21 '19 at 17:42
  • the function: `BankTransactions()` is giving limits on the length of `acctNum` and `acctPin` but the creation of the account has no checking to assure the numbers are within those limits. BTW: if the numbers are shorter than the limits, do they need leading 0's? – user3629249 Mar 21 '19 at 17:47

1 Answers1

0

Store the account number and pin separated by comma(,) and single entry in each line.

  • acount.txt

4654654688481,1234

char *account,*pin;
FILE *myfile = fopen ( filename, "r" );     
if(myfile)
{ 
      char entry [100]; //maximum line size 
      while(fgets(entry,sizeof(entry),myfile)!= NULL)
      {
        account=strtok(entry,",");
        pin = strtok(NULL,",");
        //match these with user input if matches break out of loop
      }
      fclose (myfile);
}

you can do something like this. you will able to retrieve the "account" and "pin"