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++;
}
}