an argument in stat
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > an argument in stat




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    an argument in stat  
lszk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM

My code

#include <iostream>
#include <dirent.h>
#include <sys/stat.h>
#include <map>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if((argc != 2 ){
cout << "la la la " << endl;
return 0;
}
int data = atoi(argv[2]);
const char *katalog = argv[1];
DIR* kat;
struct dirent *wynik;
struct stat info;
//    map<string, double> pliki;
kat = opendir(katalog);
while(wynik = readdir(kat) ){
[vbcol=seagreen] 

if(S_ISREG(info.st_mode)){
cout << "file" << endl;
//pliki[wynik->d_name] = info->st_ctime;
}
else cout << "catalog or sth" << endl;
}

//    map<string, double>::iterator pos;
//for(pos=pliki.begin();pos!=pliki.end();pos++);
//    cout << *pos << endl;
}

I have a problem in >>> xx<<<  line. I don't know, what I must write
there. A tryed wynik and wynik->d_name variable but it not works. I want
to send data to struct stat about file/catalog/whatever which is in
wynik variable. At the end wrote a file name with last change time to
map contener.

BTW. How can i check, when the file was created?





[ Post a follow-up to this message ]



    Re: an argument in stat  
Robert Harris


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM

lszk wrote:
> My code
>
> #include <iostream>
> #include <dirent.h>
> #include <sys/stat.h>
> #include <map>
> #include <cstdlib>
> using namespace std;
> int main(int argc, char *argv[])
> {
>     if((argc != 2 ){
>         cout << "la la la " << endl;
>         return 0;
> }
>     int data = atoi(argv[2]);
>     const char *katalog = argv[1];
>     DIR* kat;
>     struct dirent *wynik;
>     struct stat info;
> //    map<string, double> pliki;
>     kat = opendir(katalog);
>     while(wynik = readdir(kat) ){
> 
>
>     if(S_ISREG(info.st_mode)){
>                 cout << "file" << endl;
>         //pliki[wynik->d_name] = info->st_ctime;
>     }
>     else cout << "catalog or sth" << endl;
>     }
>
> //    map<string, double>::iterator pos;
>     //for(pos=pliki.begin();pos!=pliki.end();pos++);
>     //    cout << *pos << endl;
> }
>
> I have a problem in >>> xx<<<  line. I don't know, what I must write
> there. A tryed wynik and wynik->d_name variable but it not works. I want
> to send data to struct stat about file/catalog/whatever which is in
> wynik variable. At the end wrote a file name with last change time to
> map contener.
>
> BTW. How can i check, when the file was created?

The file isn't open when you call stat. Call fstat instead, viz:

ret = fstat(wynik->d_name, &info)

Robert





[ Post a follow-up to this message ]



    Re: an argument in stat  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM

Robert Harris wrote:

> lszk wrote: 
[vbcol=seagreen]
> The file isn't open when you call stat. Call fstat instead, viz:
> ret = fstat(wynik->d_name, &info)

And even that is only half way ...
you need to build up a proper filename (containing path
and filename) first ...

char *tmp;
...
tmp=malloc(strlen(katalog)+strlen(wynik->d_name)+2);
sprintf(tmp,"%s/%s",katalog,wynik->d_name);
retc=fstat(tmp,&info);
...
free(tmp);

Rainer








[ Post a follow-up to this message ]



    Re: an argument in stat  
lszk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM


> The file isn't open when you call stat. Call fstat instead, viz:
>
> ret = fstat(wynik->d_name, &info)
>
> Robert

But fstat wants in first argument a descriptor of open file. In this
code on first position is specimen struct dirent with a filename (char*)
[or sth like that]
In g++ I have
error: invalid conversion from 'char*' to 'int'
usun.cc:23: error:   initializing argument 1 of 'int fstat(int, stat*)'

I tryed (int)wynik->d_name and more, but I can do it. i must go sleep...

Please wrote me, how this line should be write.

____

Here is good with descriptor of open file.

int fd int fd = open("/home/lszk/anetta3_1.jpg", O_RDONLY);
fstat(fd, &st);
if(S_ISREG (st.st_mode))
cout << "zwykly plik" << endl;

I don't want something like that [however it not work too]

int fd;
kat = opendir(katalog);
while(wynik = readdir(kat)){
int fd = open(wynik->d_name, O_RDONLY);
fstat(fd, &info);
if(S_ISREG(info.st_mode)){
..





[ Post a follow-up to this message ]



    Re: an argument in stat  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM



Rainer Temme wrote On 06/16/06 17:21,:
> Robert Harris wrote:
>
> 
>
> 

Backwards: It's fstat() that takes an open file descriptor,
and stat() that takes a pathname.
[vbcol=seagreen]
> And even that is only half way ...
> you need to build up a proper filename (containing path
> and filename) first ...
>
> char *tmp;
> ...
> tmp=malloc(strlen(katalog)+strlen(wynik->d_name)+2);

if (tmp == NULL) ...  /* my teeth hurt if this isn't checked */

> sprintf(tmp,"%s/%s",katalog,wynik->d_name);
> retc=fstat(tmp,&info);

Should be stat instead of fstat here.

> ...
> free(tmp);

(I suspect Robert and Rainer both know better and are even
now kicking themselves.  Such is the folly of writing Usenet
posts late in the day on a Friday at the end of a long week!
Who knows?  I may yet regret writing this ...)

--
Eric.Sosman@sun.com






[ Post a follow-up to this message ]



    Re: an argument in stat  
Brian Raiter


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 12:20 AM

> The file isn't open when you call stat.

fstat() is for open files. stat() is for filenames.

To the OP: readdir() entries will only give you the bare filenames. If
you haven't changed your cwd to the directory you're reading, then you
will need to construct the full path before calling stat().

If you randomly cast things without knowing why, it's not likely to
produce a working program.

b





[ Post a follow-up to this message ]



    Re: an argument in stat  
those who know me have no need of my name


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-17-06 06:25 AM

in comp.unix.programmer i read:

>     kat = opendir(katalog);
>     while(wynik = readdir(kat) ){
> 
[vbcol=seagreen]
>I have a problem in >>> xx<<<  line. I don't know, what I must write
>there. A tryed wynik and wynik->d_name variable but it not works.

you need to compose the filename by combining katalog, "/" and
wynik->d_name.  and you should be checking stat's return value.

>BTW. How can i check, when the file was created?

unix-ish systems do not record it.

--
a signature





[ Post a follow-up to this message ]



    Re: an argument in stat  
lszk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-19-06 12:24 PM

It works:

struct stat info;
//	map<string, int> pliki;
char path[PATH_MAX + 1];
size_t path_len;
path_len = strlen(katalog);
strncpy(path, katalog, sizeof(path));
if(path[path_len - 1] != '/'){
path[path_len] = '/';
path[path_len + 1] = '\0';
++path_len;
}
kat = opendir(katalog);
while(wynik = readdir(kat)){
strncpy(path + path_len, wynik->d_name, sizeof(path) - path_len);
stat(path, &info);
if(S_ISREG(info.st_mode)){
cout << "plik" << " " << wynik->d_name << endl;
//	pliki[wynik->d_name] = info.st_ctime;
}
else cout << "katalog" << " " << wynik->d_name << endl;
}

Thx for all.





[ Post a follow-up to this message ]



    Re: an argument in stat  
lszk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-19-06 12:24 PM

I have one question more. I have problem with my map contener


typedef map<string, time_t> ddd;
ddd pliki;

char path[PATH_MAX + 1];
size_t path_len;
path_len = strlen(katalog);
strncpy(path, katalog, sizeof(path));
if(path[path_len - 1] != '/'){
path[path_len] = '/';
path[path_len + 1] = '\0';
++path_len;
}
kat = opendir(katalog);
while(wynik = readdir(kat)){
strncpy(path + path_len, wynik->d_name, sizeof(path) - path_len);
stat(path, &info);
if(S_ISREG(info.st_mode)){
//cout << "plik" << " " << wynik->d_name << endl;
char xxx[30];
//time_t statys = info.st_ctime;
strncpy(xxx, wynik->d_name, sizeof(xxx));
pliki[xxx] = info.st_ctime;
//pliki.insert(make_pair(xxx, info.st_ctime));
}
else cout << "katalog" << " " << wynik->d_name << endl;
}

/*map<string, time_t>*/
ddd::iterator pos;
for(pos=pliki.begin();pos!=pliki.end();++pos);
cout << pos->first << " " << pos->second << endl;
}
I have sig vault 11. Why? What I must change in this code ?

Something like that working:

map<string, int> mapa;
string x;
int e;
for(int i=0; i<5;i++){
cin >> x;
cin >> e;
mapa[x] = e;
}
for(pos=mapa.begin();pos!=mapa.end();pos++)
cout << pos->first << "\t" << pos->second << endl;





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:43 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register