loop inside a directory and open files

Hi everyone!
i'm new to this forum, so I hope I posted my question in the right place! I got a little problem with 'SPRINTF'. My script is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
     #include <stdio.h>
     #include <sys/types.h>
     #include <dirent.h>
     #include <iostream>
     #include <fstream>
     #include <dirent.h>
     using namespace std;
     
     int main (void)
     {
       DIR *dp;
       struct dirent *ep;
       char folder[100];
       string j;
       ifstream inn;
       const char *dirname="C:/folder/";
     
       dp = opendir (dirname);
       if (dp != NULL)
         {
           while (ep = readdir (dp))
           {
             j=ep->d_name;
             sprintf(folder,"C:/folder/%s",j);
             cout<<folder<<endl;
           }
           (void) closedir (dp);
         }
       else
       {
         perror ("Couldn't open the directory");
       }
       system("pause");
       return 0;
     }


and when I compile i get this message:

25 [Warning] cannot pass objects of non-POD type `struct std::string' through `...'; call will abort at runtime

Can anyone help me please ?!

already thank you very much
cheers Ian.
As it says, you can't pass std::string to sprintf. You should only pass char* to it. To get a char* from a string, use j.c_str();.
Though I don't think you need this at all. You can just cout << dirname << j; as you don't seem do do anything with it other than printing..
thank you for the answer!!!

actually what i'm trying to do is getting the whole path of a file inside a folder!
Even if you are going to use the concatenated string, you should use operator + overloaded for std::strings rather than sprinf.
If you use std::strings, there is really no reason to use character arrays and perform c functions on them at all.
sorry but i'm quiet new to c++ and programming in general, maybe my question will sound stupid, but what do you mean with "you should use operator + overloaded for std::strings ?
1
2
3
string a = "hello", b = "world";
string c = a + ", " + b;
cout << c;
wow, is it than simple? I've tried that various times, but it always gave me errors, I'll give it an other go !
Topic archived. No new replies allowed.