Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Stampare le info "stat" di un file

    Raga scusate ma dopo aver fatto :

    codice:
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
     struct stat stat_file;
     char *prt;
     
     if ( stat(argv[1], &stat_file) < 0)
        perror("Errore stat"), exit(1);
         
     return 0;
    }
    Come faccio a stampare uno per uno gli elementi della struttura "stat", che in questo caso è la variabile "stat_file"?

    Grazie
    Linguaggi : C/C++
    SO: WinXP, Slack 10

  2. #2
    Utente di HTML.it L'avatar di /dev/null
    Registrato dal
    May 2004
    Messaggi
    1,936
    Ti posto il codice di una spece di brutto ls che feci tempo fa:
    codice:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define DIR_TO_SCAN		"."
    
    #define	VERDE_SCURO		"\033[0;32m"
    #define	VERDE_CHIARO	"\033[1;32m"
    #define	BLU_SCURO		"\033[0;34m"
    #define	BLU_CHIARO		"\033[1;34m"
    #define	GIALLO_SCURO	"\033[0;33m"
    #define	GIALLO_CHIARO	"\033[1;33m"
    #define GRIGIO			"\033[1;30m"
    #define	RESET_COLOR		"\x1b[00m"
    
    int main ( int argc, char * argv [] ) {
    	
    	DIR * directory;
    	struct dirent * fileInDir;
    	int fd;
    	char * dirname;
    	
    	if ( argc > 2 ) {	/* FIXME: Dovro' poter scannare piu' dirs. */
    		printf ( "Troppi argomenti.\nUsage: %s [DIRECTORY]\n", argv[0] );
    		exit ( 1 );
    	} else if ( argc == 2 ) {
    		dirname = argv[1];
    	} else { /* if ( argc == 1 ) */
    		dirname = DIR_TO_SCAN;
    	}
    	
    	directory = opendir ( dirname );
    	if ( directory == NULL ) {
    		printf ( "Non posso fare un listato della directory \"%s\".\n" );
    		exit ( 1 );
    	}
    	printf ( "Ecco il contenuto della directory \"%s\":\n", dirname );
    	
    	fd = dirfd ( directory );
    	chdir ( dirname );
    	
    	while ( ( fileInDir = readdir ( directory ) ) != NULL ) {
    		struct stat data;
    			
    		printf ( "\t" );
    		
    		stat ( fileInDir->d_name, &data );
    		if ( S_ISDIR ( data.st_mode ) ) {
    			if ( fileInDir->d_name[0] == '.' || fileInDir->d_name[strlen(fileInDir->d_name)-1] == '~' )
    				printf ( "%s", BLU_SCURO );
    			else
    				printf ( "%s", BLU_CHIARO );
    		} else if ( S_ISREG ( data.st_mode ) ) {
    			if ( fileInDir->d_name[0] == '.' || fileInDir->d_name[strlen(fileInDir->d_name)-1] == '~' )
    				printf ( "%s", GRIGIO);
    		} else {
    			printf ( "%s", GIALLO_CHIARO );
    		}
    		printf ( "%s%s %d bytes", fileInDir->d_name, RESET_COLOR, data.st_size );
    		
    		
    		printf ( "\n" );
    	}
    	
    	closedir ( directory );
    	
    	return 0;
    }
    Leggiti il capitolo 5 della GaPiL: http://www.lilik.it/~mirko/gapil/gapil.html
    Ti spieghera' tutto...


    Comunque questa domanda stava meglio nel forum di programmazione


    Ultima modifica ad opera dell'utente /dev/null il 01-01-0001 alle 00:00

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.