Страница 1 из 1

функция stat.

Добавлено: 2008-07-30 12:14:31
Pal
Здравствуйте, я довольно далёк от программирования, прошу вашей помощи.
Продемонстрируйте пожалуйста работу функции stat с использованием time_t.
Что то вроде берёт аргумент(путь к файлу) и выводит её время последнего изменения.

Re: функция stat.

Добавлено: 2008-07-30 13:24:18
hizel

Код: Выделить всё

hizel@nightwatch:~/cxx$ cat teststat.c
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

char *filename="teststat";
struct stat st;

int main(int argc,char *argv[]){
        int ret;
        ret = stat(filename,&st);
        printf("%s last modified:%s\n",filename,ctime(&st.st_mtime));
        return ret;
}

Код: Выделить всё

hizel@nightwatch:~/cxx$ gcc -o teststat teststat.c

Код: Выделить всё

hizel@nightwatch:~/cxx$ ./teststat
teststat last modified:Wed Jul 30 14:23:25 2008

Re: функция stat.

Добавлено: 2008-07-30 13:35:28
hizel
и с аргументов в коммандной строке, но без защиты от дурака\хакера

Код: Выделить всё

hizel@nightwatch:~/cxx$ cat teststat.c
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

struct stat st;

int main(int argc,char *argv[]){
        int ret;
        if(argc != 2){
                printf("need one argument\n");
        }else{
                ret = stat(argv[1],&st);
                if(ret == 0){
                        printf("%s last modified:%s\n",argv[1],ctime(&st.st_mtime));
                }else{
                        printf("error stat :(\n");
                }
        }
        return 0;
}

Код: Выделить всё

hizel@nightwatch:~/cxx$ gcc -o teststat teststat.c

Код: Выделить всё

hizel@nightwatch:~/cxx$ ./teststat teststat.c
teststat.c last modified:Wed Jul 30 14:33:24 2008

Код: Выделить всё

hizel@nightwatch:~/cxx$ ./teststat none
error stat :(

Re: функция stat.

Добавлено: 2008-07-30 13:48:25
Pal
Большое спасибо.