-2

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?

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
shirin
  • 175
  • 2
  • 14
  • 1
    You'll have to post real code if you want an answer. `opendir(folder address);` for starters cannot be real; variable names cannot contain spaces. Your code has a number of error checks, but you only output to the console in the error-free path; it would help you a great deal if you added `else` blocks reporting on the error paths. Or better than that; use a debugger as step through the code. – Clifford Feb 23 '16 at 20:06
  • 1
    Rather that then testing `is_open()` and then looping while `eof()`, it is simpler to just loop `while( myReadFile.good()` which covers both and other error conditions. – Clifford Feb 23 '16 at 20:11
  • C has an `ifstream` ??? – 463035818_is_not_an_ai Feb 23 '16 at 20:12
  • folder_address is not a variable name, it is something like: c/myproject/... – shirin Feb 23 '16 at 20:29
  • You have to concatenate the `folder_address` before the `dir->d_name` in `myReadFile.open(dir->d_name);`. Because `dir->d_name` has just the filename which is not found in the current path of the executable, and hence not opened. Are all the files in the same directory or in different ones? – Ahmed Akhtar Feb 23 '16 at 20:32
  • Possible duplicate of [How can I get the list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c) – 463035818_is_not_an_ai Feb 23 '16 at 20:32
  • my problem is not c or c++, yes I know printf is for c and cout for c++ and ..., yes I can understand this, some parts of code are for c and some for c++. I will manage it in my code.... I only need your help for reading the content of the files in a folder. Then I will correct my own code! – shirin Feb 23 '16 at 20:32
  • I have read the question of this url: How can I get the list of files in a directory using C or C++? , but it is not my problem. This question only needs list of files but i need their content, too! – shirin Feb 23 '16 at 20:36

2 Answers2

1

The problem is that you open a folder at a cretain path:

 d = opendir(folder_address);  // I suppose folder_address is a string

But the d_name that you read contains the filename, and not the full path name. So if folder_address is not the current directory, the open will fail.

You have to build the full path name before trying to open the file. Try something like:

  string fullname(folder_address); 
  if (fullname.size()!=0) 
      fullname += "/";  // assuming it's posix
  fullname += dir->d_name;
  ifstream myReadFile(fullname);
  char output;
  if (myReadFile.is_open()) {  
      while (myReadFile>>output) {  // loop on read (NEVER ON EOF in C++)
          cout<<output<<endl;
      }
  }

Important remark: by the way that you shall not loop on eof, but on the reading operation on a c++ stream.

Christophe
  • 68,716
  • 7
  • 72
  • 138
1

You have to concatenate the folder_address before the dir->d_name instead of myReadFile.open(dir->d_name); using strcat.

Because dir->d_name has just the filename, which is not found in the current path of the executable, and hence not opened.

Do something like:

#include<dirent.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main(void)
{
  DIR           *d;
  struct dirent *dir;
  char folder_address[100];
  strcpy(folder_address,"c://myproject/task1");
  d = opendir(folder_address);
  if (d)
  {
   while ((dir = readdir(d)) != NULL)
   {
    if(strcmp(dir->d_name,".")==0 || strcmp(dir->d_name,"..")==0 )
        {continue;}

    ifstream myReadFile;
    char fname[200];
    strcpy(fname, folder_address);
    strcat(fname, "/");
    strcat(fname, dir->d_name);
    myReadFile.open(fname);
    char output;
    if (myReadFile.is_open())
    {
     while (1)
     {
      myReadFile >> output;
      if(myReadFile.eof())
       break;
      cout<<output<<endl;
     }
    }
        myReadFile.close();
   }
   closedir(d);
  }
  return(0);
}
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • Thank you very much. I will try it just now. – shirin Feb 23 '16 at 20:47
  • Glad to help! Welcome to SO. If this, or any other answer, solved your problem, please mark it as [accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). This makes it easier for later viewers to track down the solution to the question. – Ahmed Akhtar Feb 23 '16 at 20:54