#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 1000
#define MAX_NUM 3000
#define Num 10000
void Get_filename(char *file_path[],ifstream infile,int &n)
{static char file_pathch[MAX_NUM][MAX_LENGTH];
infile.open("g://fileDB.txt",ios::in|ios::nocreate);
if(!infile)
{cout<<"the file is not exist or open error!"<<endl;
exit(0);
}
char ch;
int i=1,j=0;
while(!infile.eof())
{infile.get(ch);
if (ch=='\n') {i++;j=0;}
else file_pathch[i][j++]=ch;
}
n=i;
for(i=1;i<=n;i++)
{file_path[i]=file_pathch[i];}
}
void main()
{ int n,i=1;
char *file_path[Num];
ifstream file,ifile;
Get_filename(file_path,ifile,n);
while(i<=n)
{file.open(file_path[i],ios::in|ios::nocreate);
cout<<file_path[i]<<endl;
if(!file)
{cout<<"error"<<endl;
i++;
continue;
}
else
{cout<<"ok"<<endl;
i++;
continue;
}
}
}
红色的file_path[i]为一指针数组中的第i个元素,指向一表示文件全路径的字符串,可能由于字符串末尾\0的缘故,始终未能执行cout<<"ok"<<endl;操作,即便计算机中确实存在要打开的文件。
请求哪位大虾帮我解决这个问题,小弟不甚感激