I have a folder and this folder has 5 text files.
I want to read these text files.
I know by this code that, I can have the list of subfolders in a folder:
#include <dirent.h>
#include <stdio.h>
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir("c://myproject/task1");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
But, I want to read these files (each files content is a character) and print this character on the screen.
So I used this code:
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir("c://myproject/task1");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(strcmp(dir->d_name,".")==0 || strcmp(dir->d_name,"..")==0 )
{continue;}
ifstream myReadFile;
myReadFile.open(dir->d_name);
char output;
if(myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
cout<<output<<endl;
}
}
}
closedir(d);
}
return(0);
}
But, I get no output.
Could you kindly help me in finding the problem in my code?