Thread: How to find a string in a text file?

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    5

    How to find a string in a text file?

    Say I have a text file called "accounts.txt" that contained the following:
    123?ric?buen
    345?anotheracc?anopwd
    45678?thisuname?thispwd
    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?
    Last edited by RicsterB; 06-18-2020 at 10:23 AM. Reason: added information

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by RicsterB View Post
    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; 
    }
    The power of null.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    Quote Originally Posted by Sir Galahad View Post
    Code:
    typedef int BOOLEAN;
    No need to reinvent the Boolean wheel. Use bool from stdbool.h instead. It's been a standard part of C for over 20 years now.

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by christop View Post
    No need to reinvent the Boolean wheel. Use bool from stdbool.h instead. It's been a standard part of C for over 20 years now.
    You think that's bad. My C++ harkens back to the Jurassic period!
    The power of null.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,883
    Quote Originally Posted by Sir Galahad View Post
    You think that's bad. My C++ harkens back to the Jurassic period!
    I first learned K and R C programming. A mixture or first and second edition.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by stahta01 View Post
    I first learned K and R C programming. A mixture or first and second edition.

    Tim S.
    Nice thing about C is it hasn't changed much over the years. C++ on the other hand, the newer stuff is hardly recognizable IMHO.
    The power of null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find the maximum value in a column of a text file
    By binbk in forum C Programming
    Replies: 8
    Last Post: 07-30-2014, 10:58 AM
  2. How to find specific line in a text file?
    By leinad0213 in forum C Programming
    Replies: 3
    Last Post: 01-18-2012, 02:37 AM
  3. How to find the longest word in text file?
    By alionas in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 01:05 PM
  4. find word in a text file
    By 26friends26 in forum C++ Programming
    Replies: 7
    Last Post: 03-01-2010, 09:37 PM
  5. Find text in quotes of string.
    By evanejk in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2010, 10:00 AM

Tags for this Thread