Funktion „calculate_units_down“ (C)

void calculate_units_down( long *a, int c, long u, long *b ) {
    
    /* USE:
    
    long a[4] = { // dividend array
    
    1000, // ms >> ss
    60, // ss >> mm
    60, // mm >> hh
    24 // hh >> dd
    
    int c = 4; // number of elements
    
    long u = 1000000000; // start unit
    
    long b[5]; // result array
    
    */
    
    b[0] = u;
    
    int i;
    
    for ( i=0; i<c; i++ ) {
        
        b[c+1] = b[c] / 1000; // e.g. ss = ms / 1000
        b[c] = b[c] % 1000; // e.g. ms = ms % 1000
        
    } // while
    
} // function
Veröffentlicht unter Beta | Verschlagwortet mit ,

Rekursives Lesen von Verzeichnissen (C)

https://stackoverflow.com/questions/8436841/how-to-recursively-list-directories-in-c-on-linux

z.B.

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

void listdir(const char *name, int indent)
{
    DIR *dir;
    struct dirent *entry;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            printf("%*s[%s]\n", indent, "", entry->d_name);
            listdir(path, indent + 2);
        } else {
            printf("%*s- %s\n", indent, "", entry->d_name);
        }
    }
    closedir(dir);
}

int main(void) {
    listdir(".", 0);
    return 0;
}

bearbeitet:

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

void listdir( const char *name, int indent ) {
	
	DIR *dir;
	struct dirent *entry;
	
	if ( !( dir = opendir( name ) ) ) return;
	
	while ( ( entry = readdir( dir ) ) != NULL) {
		
		if ( entry->d_type == DT_DIR ) {
			
			char path[1024];
			
			if ( 
			
				strcmp( entry->d_name, "." ) == 0 || 
				strcmp( entry->d_name, ".." ) == 0
				
			) continue;
			
			snprintf( path, sizeof( path ), "%s/%s", name, entry->d_name );
			
			printf( "%*s[%s]\n", indent, "", entry->d_name );
			
			listdir( path, indent + 2 );
			
		} else {
			
			printf( "%*s- %s\n", indent, "", entry->d_name );
			
		} // if/else
			
	} // while
	
	closedir(dir);
	
} // function

int main( void ) {
	
	listdir( ".", 0 );
	
	return 0;
	
} // main