0

I list the directories and then want to open the d_name of one of the entries.
For this, I input an integer i.e. the number of the item in the list. and open it's corresponding file.

I'm getting this error:

[Error] request for member 'c_str' in 'ent->dirent::d_name', which is of non-class type 'char [260]'

code:

#include <bits/stdc++.h>
#include <fstream>
#include <direct.h>
#include <Windows.h>
#include <conio.h>
#include <dirent.h>
using namespace std;
int main(){


            DIR *dir;
        struct dirent *ent;
        if ((dir = opendir ("C:/path")) != NULL) {
          /* print all the files and directories within directory */
          while ((ent = readdir (dir)) != NULL) {
            cout<<ent->d_name<<endl;
          }
        }
        else {
          /* could not open directory */
          perror ("");
          return EXIT_FAILURE;
        }
        int z;
        cout<<"enter the directory number to open"<<endl;
        cin>>z;       
        int k{1};     // counter variable
        dir = opendir ("C:/path")
        while ((ent = readdir (dir)) != NULL) {
            if(z==k){
                system((ent->d_name).c_str());
                break;
            }
            k++;
        }
}
Progman
  • 16,827
  • 6
  • 33
  • 48
anonymous38653
  • 393
  • 4
  • 16

1 Answers1

2

Remove the call to c_str(). d_name is a character array (C-style string), not a std::string type.

jkb
  • 2,376
  • 1
  • 9
  • 12
  • that answered my question, can you also help me with the further error `'hello.txt' is not recognized as an internal or external command, operable program or batch file` , hello.txt is the file that I wanted to open – anonymous38653 Jun 14 '20 at 01:40
  • @shoelace That error says you just typed `hello.txt` at the command prompt. You need to type your program name at the command prompt, then follow the prompts your program gives you. Also, if I answered your question maybe you could mark my answer as accepted? – jkb Jun 14 '20 at 03:03