load text files from directory

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
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.
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 ??
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
Thank u Peter !!
Topic archived. No new replies allowed.