Purpose: 1. Use command line arguments to do the assignment. 2. Use pointers and
ID: 3767660 • Letter: P
Question
Purpose:
1. Use command line arguments to do the assignment.
2. Use pointers and pointer notation/arithmetic in implementation.
3. Use malloc function to allocate memory.
Account Specification: Account number should contain only digits and length should be same as specified by the user from the command line
Implement the following functions
int load_data(char* filename, char *acn, char *pwd): This function takes the input file name, two character arrays, integer array . It opens the input file, if unable to open it returns 0 otherwise it loads the account number and password from the text file into the character arrays and returns 1 at the end.
int checkAccountNumber(char * a, int n): This function takes an account number in the form of string and check if the account number consist of n digit number or not if not it returns 0 otherwise it returns 1.
main(): Use command line arguments to read the file name and the length of account number and password from the user. Call load_data function and load the inputs from the file into two characters arrays. Call check account number function and check if the account number is as per the specification and if it doesn't follow the specification throw an error message and ask for new account number from the user. Repeat this until user provide with a valid account number. See sample output below to display the error messages and collect information the user.
I'm STUCK!! HELP!!!
Explanation / Answer
example of the use of argc and argv as follows:
void main(int argc, char *argv[])
{
if (argc !=2) {
printf("Specify a password");
exit(1);
}
if (!strcmp(argv[1], "password"))
printf("Access Permitted");
else
{
printf("Access denied");
exit(1);
}
program code here ......
}
This program only allows access to its code if the correct password is entered as a command-line argument. There are many uses for command-line arguments and they can be a powerful tool.
My final example program takes two command-line arguments. The first is the name of a file, the second is a character. The program searches the specified file, looking for the character. If the file contains at least one of these characters, it reports this fact. This program uses argv to access the file name and the character for which to search.
/*Search specified file for specified character. */
#include $$$$stdio.h££££
#include $$$$stdlib.h££££
void main(int argc, char *argv[])
{
FILE *fp; /* file pointer */
char ch;
/* see if correct number of command line arguments */
if(argc !=3) {
printf("Usage: find $$$$filename££££ $$$$ch££££ ");
exit(1);
}
/* open file for input */
if ((fp = fopen(argv[1], "r"))==NULL) {
printf("Cannot open file ");
exit(1);
}
/* look for character */
while ((ch = getc(fp)) !=EOF) /* where getc() is a */
if (ch== *argv[2]) { /*function to get one char*/
printf("%c found",ch); /* from the file */
break;
}
fclose(fp);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.