Aug 25, 2012 at 11:02am UTC
hey
I want to enter a folder and load all text files in that folder one by one, I wrote this func and it's wrong !!
string line;
DIR *dir;
struct dirent *pent = NULL;
dir = opendir (s.c_str());
if (dir != NULL)
{
while (pent = readdir (dir))
{
string filename = pent->d_name;
if (filename == "." || filename == "..") continue;
{
cout << "handling file: " << pent->d_name << endl;
//loadFile(pent->d_name) ;
ifstream myfile (pent->d_name);
if(myfile.is_open())
getline(myfile,line);
else
{
cout<<"Unable to open flie"<<endl;
return;
}
}
}
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
exit (1);
}
};
how to di it ????
thank u
Aug 25, 2012 at 11:14am UTC
d_name only contains the name of the file. You need to specify the (absolute or relative) path to the file when you open it.
Aug 25, 2012 at 11:24am UTC
ok , and how do i do that ??.
I want to open a folder and load every text file to a map... I don't want to write the specific path to every text file... is there a func that does that ??
Aug 25, 2012 at 11:32am UTC
You know the path to the directory where the files is located because that is what you send to opendir. Use that to calculate the file path.
1 2
string filepath = s + "/" + filename;
ifstream myfile(filepath);
Last edited on Aug 25, 2012 at 11:33am UTC