Dateien aus Verzeichnis auslesen, Datums-Wert-Problem



  • Mir werden die Dateien ausgegeben, jedoch hat mein Datum den Wert 0. Könnte mir bitte jemand sagen, wo mein Fehler liegt, ich hab jetzt schon mehrere Ansätze probiert.

    /*Inhalt eines Ordners ausgeben*/
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #ifdef __unix__
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    
    void list_dir(const char* path) {
    	DIR* dirptr;
    	struct dirent* dir;
    	struct ffblk fb;
    	if ((dirptr = opendir(path)) == NULL)
    		return;
    	while ((dir == readdir(dirptr)) != NULL)
    		printf("%s %s\n", dir.cFileName, ctime(&filestat.st_mtime));
    	closedir(dirptr);
    }
    
    #elif __WIN32__ || _MSC_VER
    #include <Windows.h>
    #include <sys/stat.h>
    
    
    void list_dir(const char* path) {
    	WIN32_FIND_DATA dir;
    	HANDLE fhandle;
    	char directory[256];
    	static char buff[256];
    	FILETIME ftCreate, ftAccess, ftWrite, ftLocal;
    	SYSTEMTIME stCreate, stNow;
    	
    
    
    	/*besser in snprintf, da sicherer*/
    	sprintf_s(directory, "%s\\*.*", path);
    	/*Handle auf das Verzeichnis*/
    	if ((fhandle = FindFirstFile(directory, &dir)) != INVALID_HANDLE_VALUE) {
    		do {
    			char fileT =GetFileTime(fhandle, &ftCreate, &ftAccess, &ftWrite);
    	
    			/*Verzeichnis auslesen*/
    			printf_s("%s %ld\n", dir.cFileName, fileT);
    		} while (FindNextFile(fhandle, &dir));
    	}
    	FindClose(fhandle);
    }
    
    #endif
    
    int main(int argc, char* argv[]) {
    
    	if (argc < 2) {
    		list_dir(".");
    	}
    	else
    	{
    		list_dir(argv[1]);
    	}
    	return EXIT_SUCCESS;
    
    }
    
    

  • Mod

    Hast du mal die Dokumentation von GetFileTime gelesen? Was gibt die Funktion zurück und was bedeuten die verschiedenen Parameter?



  • WIN32_FIND_DATA beeinhaltet schon FILETIMEs, die für jede Datei richtig besetzt werden. Für die Systemzeit gibt es dann die Funktionen FileTimeToSystemTime und SystemTimeToTzSpecificLocalTime.



  • @yahendrik ich verstehe nur nicht so ganz, wie man diese benutzt.

    printf("%s %s\n", dir.cFileName, dir.ftCreationTime);
    


  • @SeppJ Ich dachte das mir die Werte der Dateierstellung, Veränderung und letzten Nutzung ausgegeben werden.



  • Etwa so:

    SYSTEMTIME stime, local;
    FileTimeToSystemTime(&dir.ftCreationTime, &stime);
    SystemTimeToTzSpecificLocalTime(nullptr, &stime, &local);
    printf("%02d.%02d.%d  %02d:%02d",
      local.wDay, local.wMonth, local.wYear,
      local.wHour, local.wMinute);
    

    (natürlich ungetestet)



  • @yahendrik vielen vielen lieben Dank



  • Jetzt habe ich noch die Frage, wie macht man das am besten unter linux?:)



  • @Cia

    geht das damit?

    printf("%s %s\n", dir.cFileName, asctime(ts);
    

  • Mod

    Das kommt drauf an, was ts ist…



  • @SeppJ struct tm* ts; ?



  • @SeppJ

    time_t t;
    	t = time(NULL);
    	struct tm* ts;
    	ts = localtime(&t);
    	time(&t);
    

  • Mod

    Programmierst du nur, indem du Codeklötze aus dem Internet zusammen kopierst, ohne auch nur eine einzige Zeile davon zu verstehen? Was soll das mit den Zeitstempeln irgendeiner Datei zu tun haben?



  • @SeppJ die Linux sachen verwirren mich ziemlich, die werden mir auf meinem Windows Rechner auch nicht angezeigt...sry

    und ich hab auch erst angefangen und versuche es noch zu verstehen.

    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #ifdef __unix__
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    
    void list_dir(const char* path) {
    	DIR* dirptr;
    	struct dirent* dir;
    	struct tm* ts;
    	struct statfliestat;
    	char filepath[200];
    	
    	//time(&t);
    	if ((dirptr = opendir(path)) == NULL)
    		return;
    	while ((dir = readdir(dirptr)) != NULL)
    		printf("%s %s\n", dir.cFileName, ctime(&filestat.st_mtime);
    	closedir(dirptr);
    


  • hier brauchst du auch nur abschreiben:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main(int argc, char *argv[])
    {
        struct stat sb;
    
       if (argc != 2) {
            fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    
       if (stat(argv[1], &sb) == -1) {
            perror("stat");
            exit(EXIT_FAILURE);
        }
    
       printf("File type:                ");
    
       switch (sb.st_mode & S_IFMT) {
        case S_IFBLK:  printf("block device\n");            break;
        case S_IFCHR:  printf("character device\n");        break;
        case S_IFDIR:  printf("directory\n");               break;
        case S_IFIFO:  printf("FIFO/pipe\n");               break;
        case S_IFLNK:  printf("symlink\n");                 break;
        case S_IFREG:  printf("regular file\n");            break;
        case S_IFSOCK: printf("socket\n");                  break;
        default:       printf("unknown?\n");                break;
        }
    
       printf("I-node number:            %ld\n", (long) sb.st_ino);
    
       printf("Mode:                     %lo (octal)\n",
                (unsigned long) sb.st_mode);
    
       printf("Link count:               %ld\n", (long) sb.st_nlink);
        printf("Ownership:                UID=%ld   GID=%ld\n",
                (long) sb.st_uid, (long) sb.st_gid);
    
       printf("Preferred I/O block size: %ld bytes\n",
                (long) sb.st_blksize);
        printf("File size:                %lld bytes\n",
                (long long) sb.st_size);
        printf("Blocks allocated:         %lld\n",
                (long long) sb.st_blocks);
    
       printf("Last status change:       %s", ctime(&sb.st_ctime));
        printf("Last file access:         %s", ctime(&sb.st_atime));
        printf("Last file modification:   %s", ctime(&sb.st_mtime));
    
       exit(EXIT_SUCCESS);
    }
    

    und wenn du es verstehen willst, guckst du mal hier: https://linux.die.net/man/2/stat64
    da kommt das nämlich her.



  • @Wade1234 vielen Dank:)


Anmelden zum Antworten