Thread: Accessing data imported from file into the program

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    33

    Accessing data imported from file into the program

    Thanks for your help!

    Given that I have read the following into the console,
    Vladimir Ivanov 1233433 [email protected] 143343
    John Cass 132232 [email protected] 1434334343

    In the following function (remit_money), I want to be able to compare the file data to the user input (when he/she sends money, he/she obviously must identify the transferee by name). How would I access the string Vladimir?

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    #include <stdlib.h>
    #include <time.h>
     
    void startup_message();
    void login_process();
    void menu_and_startup();
    void add_transferees();
    void view_transferees();
    void remit_money();
    void thank_you_message();
     
    int main ()
    {
        startup_message();
         
        login_process();
         
        menu_and_startup();
         
        return 0;
    }
     
    void startup_message()
    {
        printf("Welcome to the newly introduced smsTransfer© (remittances through SMS text messaging)!\n");
        printf("a joint project of PioneerBank and Globe Telecom!\n");
        printf("REMINDER: Both the transferers and the transferees must have active accounts with both companies to take part in this service.\n\n");
    }
     
    void login_process()
    {
        char username[10];
        char correct_username[10];
        char second_part_of_correct_username[5];
        char firstname[10];
        char lastname[20];
        long long int cin;
        long long int correct_password;
        long long int password;
        short int attempts=0;
     
        printf("Enter your first name:\n");
        scanf("%s",firstname);
     
        strncpy(correct_username,firstname,2);
        correct_username[2]='\0';
     
        printf("Enter your last name:\n");
        scanf("%s",lastname);
     
        strncpy(second_part_of_correct_username,lastname,3);
        second_part_of_correct_username[3]='\0';
        strncat(correct_username,second_part_of_correct_username,5);
         
        printf("Enter your Customer Identification Number(CIN):\n");
        scanf("%llu",&cin);
        correct_password=(cin%10)+1;
         
        printf("Please note that usernames are case sensitive.\n");
        printf("Enter username:\n");
        scanf("%s",&username);
         
        printf("Enter password:\n");
        scanf("%llu",&password);
         
        if ((password==correct_password) && strcmp(username,correct_username)==0)
            {
                printf("Login successful!\n");
            }
         
        while ((password!=correct_password) || strcmp(username,correct_username)!=0)
            {
                printf("\nThe login details are invalid.Please try again.\n");
                 
                printf("Enter username:\n");
                scanf("%s",&username);
     
                printf("Enter password:\n");
                scanf("%llu",&password);
                 
                attempts++;
                 
                if (attempts>=5)
                    printf("Too many tries. Please re-enter the program and attempt to complete the login process again.\n");
                if (attempts>=5)
                    exit(0);    
                 
                if ((password==correct_password) && strcmp(username,correct_username)==0)
                    printf("Login successful!\n");
            }
    }
     
    void menu_and_startup()
    {
        short int option;
        do
        {
        printf("___________________________________________\n");
        printf("1. Add transferees\n");
        printf("2. View current transferee list\n");    
        printf("3. Remit money\n");
        printf("4. Account information and settings\n");
        printf("5. Exit the application\n");
        printf("____________________________________________\n");
        printf("Please choose from the above options: \n");
        scanf("%d",&option);    
        switch(option)
            {
                case 1: add_transferees();
                        break;
                case 2: view_transferees();
                        break;
                case 3: remit_money();
                        break;
                case 4: add_transferees();
                        break;
                case 5:    thank_you_message();
                        exit(0);
            }
        } while(option>0 && option<6);
    }
     
    void add_transferees()
    {
      FILE *file;
      int i;
      char firstName[32];
      char lastName[32];
      char email_address[40];
      long long int account_number;
      long int phone_number;
      short int addchoice;
      int found = 0;
     
      file = fopen("transferee_list.txt", "a");
      if (!file)
      {
        printf("File could not be opened\n\a\a");
        getchar();
        exit(-1);
      }
       
      printf("Enter number of transferees you wish to add:\n");
      scanf("%d",&addchoice);
       
      for (i = 0; i<addchoice; ++i)
      {
        printf("Transferee #%d\n", i+1);
         
        printf("Enter first name:\n"); scanf("%s", &firstName);
        printf("Enter last name:\n"); scanf("%s", &lastName);
        printf("Enter PioneerBank account number:\n"); scanf("%llu", &account_number);
        printf("Enter e-mail address:\n"); scanf("%s", &email_address);
        printf("Enter Globe Telecom phone number:\n"); scanf("%lu",&phone_number);
        printf("\n");
     
        fprintf(file, "%s\t%s\t%llu\t%s\t%lu\n", firstName, lastName,account_number,email_address,phone_number);
      }
       
      printf("Transferee list successfully updated!\n");
       
      fclose(file);
    }
     
    void view_transferees()
    {
      char firstName[32];
      char lastName[32];
      char email_address[40];
      long long int account_number;
      long int phone_number;
      int found=0;
       
      FILE *file;
      file = fopen("transferee_list.txt", "r");
      if (!file)
      {
        printf("File could not be opened\n\a\a");
        exit(-1);
      }
     
      else
      {
        fscanf(file, "%s\t%s\t%llu\t%s\t%lu", &firstName,&lastName,&account_number,&email_address,&phone_number);
        {
          printf("Current transferees:\n First Name:%s\n Last Name:%s\n Account Number:%llu\n E-mail address:%s\n Phone Number:%lu\n", firstName, lastName, account_number, email_address, phone_number);
          found = 1;
        }
      }
      if (!found)
        printf("Record could not be found");
     
      fclose(file);
    }
     
    void remit_money()
    {
        char transferee_name[30];
        float current_balance;
        float transfer_amount;
         
        printf("\nCurrent chequing amount balance:%.2f",current_balance);
        printf("Please enter the transferee name:");
        scanf("%s",&transferee_name);
        printf("\nEnter the desired transfer amount:$");
        scanf("%.2f",&transfer_amount);
         
        if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)    
            current_balance-=transfer_amount;
        while (transfer_amount>=5000)
        {
            printf("In accordance with the Mobile Banking Act of 2024, customers are prohibited from sending amounts in excess of $5,000 per transaction.");
            printf("\nEnter the desired transfer amount:$");
            scanf("%.2f",&transfer_amount);
             
            if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)    
                    current_balance-=transfer_amount;
        }    
    }
     
    void thank_you_message()
    {
        printf("Thanks for choosing and using smsTransfer©!\n");
    }

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    You can:
    1. open the file with fopen()
    2. loop through the lines in the file with fgets()
    3. compare first word in line to the given string

    BTW, I don't know if you noticed, but I tried to compile your code and there were a lot of warnings.
    I personally prefer a code clean from warnings, and I would advice not to ignore them.

    Code:
    main.c: In function ‘login_process’:
    main.c:64:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
      scanf("%s", &username);
      ^
    main.c:79:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
       scanf("%s", &username);
       ^
    main.c: In function ‘menu_and_startup’:
    main.c:108:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘short int *’ [-Wformat=]
       scanf("%d", &option);
       ^
    main.c: In function ‘add_transferees’:
    main.c:148:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘short int *’ [-Wformat=]
      scanf("%d", &addchoice);
      ^
    main.c:154:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[32]’ [-Wformat=]
       scanf("%s", &firstName);
       ^
    main.c:156:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[32]’ [-Wformat=]
       scanf("%s", &lastName);
       ^
    main.c:160:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[40]’ [-Wformat=]
       scanf("%s", &email_address);
       ^
    main.c:138:6: warning: unused variable ‘found’ [-Wunused-variable]
      int found = 0;
          ^
    main.c: In function ‘view_transferees’:
    main.c:191:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[32]’ [-Wformat=]
         &account_number, &email_address, &phone_number);
         ^
    main.c:191:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[32]’ [-Wformat=]
    main.c:191:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘char (*)[40]’ [-Wformat=]
    main.c: In function ‘remit_money’:
    main.c:213:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat=]
      scanf("%s", &transferee_name);
      ^
    main.c:215:2: warning: unknown conversion type character ‘.’ in format [-Wformat=]
      scanf("%.2f", &transfer_amount);
      ^
    main.c:215:2: warning: too many arguments for format [-Wformat-extra-args]
    main.c:223:3: warning: unknown conversion type character ‘.’ in format [-Wformat=]
       scanf("%.2f", &transfer_amount);
       ^
    main.c:223:3: warning: too many arguments for format [-Wformat-extra-args]
    main.c:211:8: warning: ‘current_balance’ is used uninitialized in this function [-Wuninitialized]
      printf("\nCurrent chequing amount balance:%.2f", current_balance);
    Last edited by Absurd; 05-15-2015 at 05:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing data declared private within header file
    By googol in forum C++ Programming
    Replies: 11
    Last Post: 02-11-2015, 10:51 PM
  2. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  3. How to stop a program from accessing the file system
    By gogoc in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2008, 06:09 PM
  4. findmax from imported file
    By sugie in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2005, 03:57 PM
  5. Accessing data in the program stack
    By dajur in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 10:15 PM