0

I have a perfectly working piece of code in C which I want to modify, as an exercise.

Among this code there is a call to a function readMatrix whose arguments are passed from argv. Here is the relative code

int main(int argc, char **argv){
    int dim;
    struct sparseMatrix sparse;

    readMatrix(argv[1], &sparse, &dim);
    return 0;
}

The readMatrix function is defined like this

int readMatrix(char *path, struct sparseMatrix *mat, int *n)

and it contains also the following check

FILE *f = fopen(path, "r");

if(f == NULL){
    puts("File not found!");
    return -1;
}

What I'm trying to do is to call the same function, without modifying it, getting the parameters from stdin instead of from argv. I tried the following, but without success:

int main(){
    int dim;
    struct sparseMatrix sparse;

    char nameMatrix[80];
    char *a;
    a = nameMatrix;

    puts("Insert the name of the matrix");
    fgets(nameMatrix,80,stdin);

    readMatrix(a, &sparse, &dim);
    return 0;
}

It compiles without warnings or errors, but I get "File not found!" from readMatrix. From my understanding I'm having problems with the array containing the name of the file (nameMatrix) and its pointer. What am I doing wrong?

Thank you very much and sorry for the bad formatting, I'm still quite new to posting questions.

Jado
  • 5
  • 4

0 Answers0