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

>else DIR * mydir = opendir(dir);
Note the else without braces. This means you have an else clause with one statement that defines a variable and then immediately destroys it. It's as if you had braces like so:

else
{
    DIR * mydir = opendir(dir);
}

mydir doesn't exist outside of implied block. Your other error is because std::string doesn't have an implicit conversion to const char* . The call to opendir() should be more like opendir(dir.c_str()) .

i nested everything in the starting else{} braces and it works, so thanks for that. is this what u intended me to do?

...
    else
    {
        DIR * mydir = opendir(dir.c_str());
        if (!mydir)
            {
        cout << "Failed opening directory\n";
        exit(1);
            }
        else cout << "Directory opened successfully\n";

        struct dirent* entry;
        cout << "Contents of directory saved in " << filename << endl;
        while ((entry = readdir(mydir)) != NULL)
            {
            newfile << entry->d_name << endl;
            }
        closedir(mydir);
        cout << "Dir closed successfully\n";
    }
    return 0;
}

also, any idea why the getline() section from my first post doesnt work?

i remove the cin >> command on line 21 and uncomment line 22; when i run the program it saves the input for 'dir' properly, but it then directly skips to the end of the program making a blank .txt file with the details of the directory specified, it doesnt allow the user to type a file name

cheers in advance

struct dirent must not be declared as a pointer

struct dirent entry;
 while ((entry = readdir(&mydir)) != NULL)

Pointers are not rocket science. It is actually an a variable storing the adress of the variable you are pointing.

int n = 10; // this is an ordinary variable
int *a = &n; // this sentence means a is a pointer to an integer and evaluated to the address of n

It is as simple as that. What you must really understand is why we need to use pointers.

Do a search about it. And do lot of practices like sorting, printing, editing array like data by using pointers. Passing data back and forth between functions.

One suggestion with your changed if/else that you were working on.

If the program fails to open the file, might as well close the program with

exit(1);

at which point you do not need to worry about an else. If it doesn't fail, the program keeps on going leaving the block that you had to worry about nonexistent (If you don't need an else, you don't need a block for that else).

And for pointers, you will naturally understand them with more practice. It seems like the few references that you did look over may not have been adequate for you. There are a lot of "tutorials" and books out there that are not well written or for a very specific purpose that may be hard to comprehend. Keep it up though. Once you learn how pointers work, you will have a blast with dynamic arrays, classes, etc. :D

ok thanks for the feedback guys, i will continue reading :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.