turning a string in to a character

So I'm kinda new at this and I'm using a function I found which I don't entirely understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}


I am trying to write a program which will parse a directory full of images, then load them.

Here is the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char *argv[])
{



    string dir = string(argv[1]);
    vector<string> files = vector<string>();
    getdir(dir,files);




    for (unsigned int i = 0;i < files.size();i++) {

    IplImage *img = cvLoadImage(files[i],0);
    cout << files[i] <<","<<dotcount(files[i]) endl;
    cvReleaseImage(&files[i]);

    }

    return 0;
}


I'm getting errors trying to load the file with cvLoadImage. It says I can't convert a string to a character.
Last edited on
It's not asking for a character, it's asking for a pointer to character data.

This can be obtained with the c_str() member function:

IplImage *img = cvLoadImage( files[i].c_str() ,0);
Topic archived. No new replies allowed.