|
Home > Archive > Unix Programming > June 2006 > an argument in stat
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
an argument in stat
|
|
|
| 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?
| |
| Robert Harris 2006-06-16, 7:20 pm |
| 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
| |
| Rainer Temme 2006-06-16, 7:20 pm |
| 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
| |
|
|
> 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)){
...
| |
| Eric Sosman 2006-06-16, 7:20 pm |
|
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
| |
| Brian Raiter 2006-06-16, 7:20 pm |
| > 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
| |
| those who know me have no need of my name 2006-06-17, 1: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
| |
|
| 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.
| |
|
| 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;
|
|
|
|
|