Hello, I am new to this site and relatively new to programming, so bear with me. As the title explains, I have an ASCII file that an user will pass as a command line parameter and I need to read this file of ASCII values into a dynamic array that is the same size as the file. Then, I need to scan the file for a given character based off of the user's input value (0-255). Here is what I have so far.
I have left out the next bit of code because it consists of nothing but a menu that allows the user to select what action they want to take place. However, if they choose the function to scan for a specific character in the file, this is what runs.Code:int main(int argc, char *argv[]) { char* asciiArray; int numASCII = 0; int i = 0; int selection = 0; FILE* fp; // Check to see if user inputted the correct amount of commands if (argc != 3) { printf("ERROR: Incorrect amount of command line parameters."); exit(1); } else { numASCII = atoi(argv[1]); asciiArray = calloc(numASCII, sizeof(char)); // dynamically allocates memory in array if (asciiArray == NULL) { printf("ERROR: Memory could not be allocated."); exit(1); } // Opens the file that the user provided fp = fopen(argv[1], "r"); if (fp == NULL) { printf("ERROR: Could not open file."); exit(1); } // Reads content of input file into the declared dynamically allocated array for (i = 0; i < numASCII; i++) { fscanf(fp, "%c", asciiArray + i); } }
So far this is my educated guess on how to do this, and I am not confident that it is correct however. Help would be greatly appreciated. Thank you.Code:char asciiChar; int value; int success = 0; int i = 0; printf("Input a decimal value (0-255): "); success = scanf("%d", &value); if (success != 1) { printf("ERROR: Invalid decimal value - not a numeric value."); exit(1); } if (value < 0 || value > 255) { printf("ERROR: Invalue decimal value - outside allotted range."); exit(1); } for (i = 0; i < numASCII; i++) { if (asciiArray[i] == (char)value) { asciiChar = asciiArray[i]; } }



LinkBack URL
About LinkBacks



