0

Background:

I am using Visual Studio 2019 to program in c for a class assignment. For the assignment I have some command line arguments that are passed in. One of the command line arguments is a text file. For the assignment we have to read this text file and do some math with the list of numbers. The exact command line arguments are "FIFO 10 diskData.txt" which are entered in the properties of the program. I have both prog3.c and diskData.txt in the Source Files folder of the project.

Error:

Whenever I run the program I get this error. When I did some debugging, the file pointer was still null after opening a file. If I continue it fails on line 39. It says "Exception Unhandled: An invalid parameter was passed to a function that considers invalid parameters fatal." I'm pretty sure that invalid parameter is the input file being null.

Code

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int main(int argc, char* argv[])
{
    // Check to amke sure there are exactly 3 user inputs
    if (argc == 4)
    {
        char* algorithmType = argv[1]; // The first command line argument is the algorithm to use
        int const queueSize = atoi(argv[2]); // Second command line argument is the queue size
        char * inputFile = argv[3]; // Third command line argument is the file to process

        int queue[50]; // Define the queue to be used as a buffer and define the size as the max possible input because couldn't set it to the variable queueSize

        FILE* input; // Open the file given
        input = fopen(inputFile, "r");

        // Check to see if the file was opened
        printf("%s\n%d\n%s\n", algorithmType, queueSize, inputFile);

        // Checks to see if the input algorithm is C-SCAN
        if (strcmp(algorithmType, "C-SCAN") == 0)
        {
            printf("C-SCAN was chosen");
        }

        // Checks to see if the input algorithm is FIFO
        if (strcmp(algorithmType, "FIFO") == 0)
        {
            printf("FIFO was chosen\n");

            int front = 0; // marks the front of the queue to pull from
            int rear = 0; // marks the rear of the queue add stuff to
            char line[100]; // Lines pulled from the file are stored here

            // Scans the file one line at a time until the end of the file
            while (fgets(line, 100, input) != NULL)
            {
                queue[rear] = atoi(line); // Converts line to int and stores it
                printf("%d", queue[rear]);
                rear++;

                // Checks if the queue is full based on the requested size
                if (rear == queueSize)
                {
                    printf("filled queue\n");
                }
            }

        }

        // Checks to see if the input algorithm is SSTF
        if (strcmp(algorithmType, "SSTF") == 0)
        {
            printf("SSTF was chosen");
        }

        fclose(input);
    }
    // If they didn't put in 3 inputs
    else
    {
        printf("Given %i arguments. Incorrect number of arguments. Looking for 3 arguments.", argc - 1);
    }
}

The first line is to surppress warnings I kept getting about using fopen_s instead of fopen. But I'm not sure if that would work properly when I send it to my professor. I also couldn't find any documentation about fopen_s.

Question?

Any idea why the file pointer is null? Also curious if the suppression on line 1 would cause issues if I sent the c file to my professor?

Any help is appreciated!

0 Answers0