Hello all: My work need to read files into arrays. In the past, only
several files,I set a file pointer and used fscanf with following codes
(supose each file only has 3-number in three lines):
When running the above codes, the files are in the same folder of cppCode:if((fp=fopen("file1.txt", "r"))==NULL) {cout<<"can not open file 1"<<endl;); return; } for(int i=0; i<3; i++) fscanf(fp,"%f", &arr[0][i]); //read file1 if((fp=fopen("file2.txt", "r"))==NULL) {cout<<"can not open file 2"<<endl;); return; } for(i=0; i<3; i++) fscanf(fp,"%f", &arr[1][i]); //read file2
source codes. The above codes work well except reading files one by one.
Now the number of files become more. I tried to use loop to avoid one
by one. But failed! The following is the codes.
This codes also can pass compiling and linking, but the resultCode:char textfile[3][10]={"file1.txt","file2.txt","file3.txt"}; for(j=0; j<3; j++) { if((fp=fopen("textfile[j]", "r"))==NULL) {cout<<"can not open file "<<j<<endl; return; } for(i=0; i<3; i++) fscanf(fp,"%f", &arr[j][i]); }
of excuting is allways: "can not open file[0]"!
Clearly, "file1.txt" can not be changed to "textfile[0]", although
textfile[0]=file1.txt;"file2.txt" can not be changed to "textfile[1]",
although textfile[1]=file2.txt;..... Why?
Who can help me with the problem? Or suggest me other good way for
reading files.



LinkBack URL
About LinkBacks



) to the filename: