Costas Koutsoumpas
Costas Koutsoumpas

Reputation: 1

Opening file issue

I have a problem with my code and I hope you can help. I won't post all the code because it's a little long, but I will focus on the troublesome part. The program is about algebraic operation with arrays. The read_array2 reads the first nxn real numbers from a file and place them in a global array. The problem is that I always get the same message "I cannot open this file". I even added a printf() to test if the name is properly stored in fname and I found that it is. And yes, I have verified that the files I use exist and I type their names correctly. I don't know if it is important, but the files have 10000 real numbers separated by a single space. I know it is difficult to tell without all the code available, but do you see something out of place that could cause this issue?

int read_array2(double table[N_MAX][N_MAX], int n){
int i, j;
FILE *infile;
char fname[25];

printf("\nFile Name: ");
scanf("%s",&fname);

if ((infile = fopen(fname,"r")) == NULL){
    printf("\nI cannot open this file.\n");
    return 1;
}

for (i = 0; i < n; i++)
    for(j = 0; j < n; j++){
    fscanf(infile, "%lf ", &table[i][j]);
}

fclose(infile);

return 0;
}

Upvotes: 0

Views: 46

Answers (1)

Ulrick
Ulrick

Reputation: 33

Did you remember to type in the extension of the file as well? For example: to open a text file called "MyFile" you need to enter "MyFile.txt" Also if it's not in the same directory of your program, you will need to include the full path as well. Note: The file names are case sensitive and when typing in a path you need to escape the escapes "\\" or use '/' i.e. C:\\Users\\MyFile.txt or C:/Users/MyFile.txt

Upvotes: 1

Related Questions