Thread: How do I scan an ASCII file in an array with the same size as user's input file?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    How do I scan an ASCII file in an array with the same size as user's input file?

    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.

    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);
            }
        }
    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:
         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];
             }
         }
    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.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably you want
    Code:
    fp = fopen(argv[2], "r");
    In what sense do you want to "scan the file for a given character", i.e., what do you want to do:
    1. count how many times it occurs?
    2. find if it occurs at all in the array?
    3. find the location of the first occurrence in the array?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Quote Originally Posted by oogabooga View Post
    Presumably you want
    Code:
    fp = fopen(argv[2], "r");
    In what sense do you want to "scan the file for a given character", i.e., what do you want to do:
    1. count how many times it occurs?
    2. find if it occurs at all in the array?
    3. find the location of the first occurrence in the array?
    Thank you.
    I want to scan for if it character appears at all within the file. If it does the address and 16 bits should be displayed.


    For example, if the character 67 (‘C’) is searched for and is located at
    address 7654321, the output should show (16 bytes):
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
    [7654320] [ ~ C L E M S O N T I G E R S ! ]
    7E 43 4C 46 4D 53 4F 4E 20 54 49 47 45 52 53 21

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-specified input file issue.
    By ajrey92 in forum C Programming
    Replies: 5
    Last Post: 01-10-2011, 05:16 PM
  2. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  3. Array size base on user input
    By icoigo in forum C Programming
    Replies: 2
    Last Post: 10-27-2010, 12:28 PM
  4. Replies: 3
    Last Post: 08-28-2010, 10:21 PM
  5. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM