Hello,
I am trying to create an array of file names such that when I want to open one of the files, who's name is given in the array, I can call that element of the array.
My code for making the array is the following,
Code:char file_name[40]; char *file_locations[N]; // array of N names (*) for(ii=0; ii<=N-1; ii++){ // printing the names of all the files P1_8mag_1.txt - P1_8mag_N.txt sprintf(file_name, "P1_8mag_%i.txt", ii+1); file_locations[ii] = file_name; }
then to open a given file I run
Code:FILE*fp1=fopen(file_locations[i], "r"); // open the file
The problem with this is that in the first step: all elements of the array "file_locations" are the name of the last file in the loop (in this case "P1_8mag_N.txt")
What is wrong with this?
Ps thanks for all of you comments, I am new to C and programming.
BN



1Likes
LinkBack URL
About LinkBacks



