PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : stat() - Datum und Zeit von Datei auslesen



jay-t
07-08-2010, 18:50
Hallo, ich versuche gerade mit stat() die Zeit und das Datum der letzten Dateimodifikation auszulesen:

In diese Struktur:



struct time
{
U1 hour;
U1 min;
U1 sec;
};

struct date
{
U4 year;
U1 month;
U1 day;
};

struct memory
{
U1 type;
U4 hash;
void *data;
U8 linknum;
U8 *links;
U1 strenght;
struct time time;
struct date date;
struct date expires;
};



Hier ist die Funktion:


U1 set_time_from_file (U8 index, U1 *file)
{
struct stat buf;

if (! stat (file, buf))
{
return (FALSE);
}

tm = localtime (buf->st_mtime);

memory[index].time.hour = tm->tm_hour;
memory[index].time.min = tm->tm_min;
memory[index].time.sec = tm->tm_sec;

memory[index].date.year = tm->tm_year + 1900;
memory[index].date.month = tm->tm_mon +1 ;
memory[index].date.day = tm->tm_mday;

#if DEBUG
printf ("set_time_from_file:\n");
printf ("time: %i:%i:%i\n", memory[index].time.hour, memory[index].time.min, memory[index].time.sec);
printf ("date: %i.%i.%i\n", memory[index].date.day, memory[index].date.month, memory[index].date.year);
#endif

return (TRUE);
}


Das gibt haufenweise Fehlermeldungen:



time.c:62: warning: assignment makes pointer from integer without a cast
time.c:64: error: subscripted value is neither array nor pointer
time.c:64: error: dereferencing pointer to incomplete type
time.c:65: error: subscripted value is neither array nor pointer
time.c:65: error: dereferencing pointer to incomplete type
time.c:66: error: subscripted value is neither array nor pointer
time.c:66: error: dereferencing pointer to incomplete type


Wie geht das richtig? Was ist falsch?

undefined
07-08-2010, 19:01
Fehlende Referenz auf &buf in funktion stat und falsche Referenzierung weiter dahinter.


/** int stat(const char *path, struct stat *buf);
stats the file pointed to by path and fills in buf. */
if ( stat(file, &buf) == -1 )
return EXIT_FAILURE;

jay-t
08-08-2010, 11:20
Danke mal!

Warum geht das nicht mit dem Punkt Operator?
Wie z.B. hier:



memory[index].time.hour = tm->tm_hour;


Kannst du mir da mal helfen?

jay-t
09-08-2010, 10:33
Bei dem hier gibt es keine Fehlermeldung:



memory[index].time.hour = 11;


Also muß am anderen Teil "tm->tm_hour" ein Fehler sein.
Ich habe im Netz nach Codeschnipseln gesucht, dort wird immer so auf die Struktur "tm"
zugegriffen.

Ich finde den Fehler einfach nicht.

undefined
09-08-2010, 13:20
Weil im Srtcut von stat wie auch im struct memory keine referenz auf time_t steht.
Siehe die Manpages man stat


struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};

jay-t
09-08-2010, 13:57
Die Struktur tm ist global als "struct tm *tm" definiert.

Für was brauche ich "time_t"?
Localtime liefert doch die Struktur "tm" zurück.

undefined
09-08-2010, 19:16
Vielleicht verstehst du dann besser was ich meine ;)


/*
touch --reference=/etc/fstab /tmp/testfile.txt
gcc -Wall -o timetest timetest.c
./timetest
*/

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>

int main ( void )
{
const char* file = "/tmp/testfile.txt";
struct stat p_stat;
struct tm* m_time;

if ( stat ( file, &p_stat ) == -1 )
{
perror ( "stat" );
return EXIT_FAILURE;
}

m_time = localtime ( &p_stat.st_mtime );

printf ( "Ausgabe: %i:%i:%i\n",
m_time->tm_hour,
m_time->tm_min,
m_time->tm_sec
);

return EXIT_SUCCESS;
}

jay-t
09-08-2010, 20:49
Ok! Jetzt geht es, es lag an einer fehlenden Includedatei.
Danke für deine Geduld.