the aim of this programme is to create a .txt file (of which the name is specified by the user) of all the items in a certain directory (also specified by the user)
the problem is about half way down, the 'mydir' bit
#include <iostream>
#include <fstream>
#include <string>
#include "dirent.h"
#include <cstdlib>
using namespace std;
int main()
{
//input section
string dir;
string filename;
cout << "Please enter the directory you would to enquire about: \n";
cin >> dir;
cout << "Your selected directory was: " << dir << endl;
cout << "Please choose a name for your list file: \n";
cin >> filename; // works, but only allows filename to be one word long
//getline(cin, filename); <------ should fix 1 word limit, but it doesn't work
filename += ".txt";
cout << "Your selected file name is: " << filename << endl;
//beginning of file stream
ofstream newfile (filename.c_str(), ios::app);
if (!newfile)
{
cout << "Failed to open file: " << newfile << endl;
newfile.close();
}
else DIR * mydir = opendir(dir); // <--- everything works until this point
// 'dir' variable is the input from earlier
if (!mydir)
{
cout << "Failed opening directory\n";
exit(1);
}
else cout << "Directory opened successfully\n";
struct dirent* entry;
cout << "Contents of directory:\n";
while ((entry = readdir(mydir)) != NULL)
{
cout << entry->d_name << endl;
}
closedir(mydir);
cout << "Dir closed successfully\n";
*/
return 0;
}
Error codes:
In function 'int main()':|
|41|error: cannot convert 'std::string' to 'const char*' for argument '1' to 'DIR* opendir(const char*)'|
|41|warning: unused variable 'mydir'|
plus 3 more errors where 'mydir' apparently isnt declared in this scope
the main error in this code was based on something i copied from a directory tutorial so i dunno why it wont work. ive stared at it for ages n googled to my hearts content with no success, im new to programming so try not to use too many big words when explaining ;)
also i still dont really understand pointers even though ive read like 5 different books/websites explaining them, and ive got a feeling the problem has something to do with them
any help is appreciated