Search code examples
cparameter-passingargvargc

Passing argv and argc to be able to read any file name of any file type


I am trying to change my program from able to read a hard coded name.txt to of any name or readable type. I am having trouble passing argc and argv correctly and is giving errors such as "too few arguments in main" I tried to initialize the two in the function that was used, but is undetermined. How would I correctly use these two tools? (arrows points to arg lines) Here is my code:

#include <stdio.h>
#include <stdlib.h>

// Pointer to store numbers
int *pointerNUM;

// Read in the parts file and returns the length
int readFile(int argc, char *argv[])      //<-----------------------------
{
    // File pointer
    FILE *fptr;
    // numberOfNUM for number of numbers
    // cntVAR for counter variable
    int numberOfNUM, cntVAR;
    // Open the file for reading
    fptr = fopen(argv[1], "r"); //<-----------------------------------------

    // Check that it opened properly
    if (fptr == NULL)
    {
        printf("Cannot open file \n");
        exit(0);
    }// End of if condition

    // Reads number of numbers in the file
    fscanf(fptr, "%d", &numberOfNUM);

    // Dynamically allocates memory to pointer pointerNUM
    pointerNUM = (int *) calloc(numberOfNUM, sizeof(int));
    // Loops numberOfNUM times
    for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
        // Reads each number and stores it in array
        fscanf(fptr, "%d", &pointerNUM[cntVAR]);
    // Returns the length of the numbers
    return numberOfNUM;
}// End of function

// Function to display numbers
void display(int numberOfNUM)
{
    int cntVAR;

    // Loops numberOfNUM times
    for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
        // Displays each number
        printf("%4d, ", pointerNUM[cntVAR]);
}// End of function

// Function for insertion sort
void insertionSort(int numberOfNUM)
{
    int x, key, y;

    // Loops numberOfNUM times
    for (x = 1; x < numberOfNUM; x++)
    {
        // Stores i index position data in key
        key = pointerNUM[x];
        // Stores x minus one as y value
        y = x - 1;

        /*
        Move elements of pointerNUM[0..x - 1], that are greater than key,
        to one position ahead of their current position
        */
        while (y >= 0 && pointerNUM[y] > key)
        {
            // Stores pointerNUM y index position value at pointerNUM y next index position
            pointerNUM[y + 1] = pointerNUM[y];
            // Decrease the y value by one
            y = y - 1;
        }// End of while
        // Stores the key value at pointerNUM y plus one index position
        pointerNUM[y + 1] = key;
    }// End of for loop
}// End of function

// main function
int main()
{
    // To store the numbers of number in file
    int numberOfNUM;

    // Calls the function to read numbers and stores the length
    numberOfNUM = readFile(); <-----------------------------------------
    // Calls the function to displays the numbers before sorting
    printf("\n Before sort : ");
    display(numberOfNUM);
    // Calls the function for sorting
    insertionSort(numberOfNUM);
    // Calls the function to displays the numbers after sorting
    printf("\n After sort: ");
    display(numberOfNUM);
}

Solution

  • Your prototype for the main() function is wrong. If you want to use argc and argv in C you need to use the version of main that supports them.

    int main(int argc, char *argv[])
    {
       // Check argc, and argv for correctness
    
       numberOfNum = readFile(argc, argv);
       ...
    }
    

    Then to run it do:

    ./myProgram arg1 arg2 ... argN