Archive for the ‘C Programming Language’ Category
read directory with c programming
ขอบคุณ http://www-128.ibm.com/developerworks/aix/library/au-unix-readdir.html
/* readdir() and friends demo
*
* Demo program showing how to read the contents of a directory
* and do useful things
*
*/#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>/* process_directory( char *dir )
*
* Walk through the given directory and do things to the
* entries inside. Returns the number of directory entries
* processed.
*
* Recursively processes directories.
*/
unsigned process_directory( char *theDir )
{
DIR *dir = NULL;
struct dirent entry;
struct dirent *entryPtr = NULL;
int retval = 0;
unsigned count = 0;
char pathName[PATH_MAX + 1];/* Open the given directory, if we can. */
dir = opendir( theDir );
if( dir == NULL ) {
printf( "Error opening %s: %s", theDir, strerror( errno ) );
return 0;
}retval = readdir_r( dir, &entry, &entryPtr );
while( entryPtr != NULL ) {
struct stat entryInfo;if( ( strncmp( entry.d_name, ".", PATH_MAX ) == 0 ) ||
( strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) ) {
/* Short-circuit the . and .. entries. */
retval = readdir_r( dir, &entry, &entryPtr );
continue;
}(void)strncpy( pathName, theDir, PATH_MAX );
(void)strncat( pathName, "/", PATH_MAX );
(void)strncat( pathName, entry.d_name, PATH_MAX );if( lstat( pathName, &entryInfo ) == 0 ) {
/* stat() succeeded, let's party */
count++;if( S_ISDIR( entryInfo.st_mode ) ) { /* directory */
printf( "processing %s/\n", pathName );
count += process_directory( pathName );
} else if( S_ISREG( entryInfo.st_mode ) ) { /* regular file */
printf( "\t%s has %lld bytes\n",
pathName, (long long)entryInfo.st_size );
} else if( S_ISLNK( entryInfo.st_mode ) ) { /* symbolic link */
char targetName[PATH_MAX + 1];
if( readlink( pathName, targetName, PATH_MAX ) != -1 ) {
printf( "\t%s -> %s\n", pathName, targetName );
} else {
printf( "\t%s -> (invalid symbolic link!)\n", pathName );
}
}
} else {
printf( "Error statting %s: %s\n", pathName, strerror( errno ) );
}retval = readdir_r( dir, &entry, &entryPtr );
}/* Close the directory and return the number of entries. */
(void)closedir( dir );
return count;
}/* readdir_demo main()
*
* Run through the specified directories, and pass them
* to process_directory().
*/
int main( int argc, char **argv )
{
int idx = 0;
unsigned count = 0;for( idx = 1; idx < argc; idx++ ) {
count += process_directory( argv[idx] );
}return EXIT_SUCCESS;
}
convert double to array with c programming
ขอบคุณ http://www.gidforums.com/t-2655.html?page=2
#include
int main(){
double number;
char string[22];
number = 37.5000 * 1.5000;
printf(“Your number is: %20.4f\n”,number);
sprintf(string,”%20.4f”,number);
printf(“Your number is %s\n”,string);
return 0;
}
strtok, strtok_r
strtok, strtok_r – split string into tokens
char *strtok(char *restrict s1, const char *restrict s2);
#include
…
char *token;
char *line = “LINE TO BE SEPARATED”;
char *search = ” “;
/* Token will point to “LINE”. */
token = strtok(line, search);
/* Token will point to “TO”. */
token = strtok(NULL, search);
FSETPOS()
FSETPOS – seek to new read/write position.
(ANSI Standard)
Usage:
#include <stdio.h> stat = fsetpos(stream,pos);
Where:
- FILE *stream;
- indicates the stream that you wish to reposition.
- const fpos_t *pos;
- points to positioning information as returned by “fgetpos”.
- int stat;
- is zero if “fsetpos” is successful, and non-zero otherwise.
Description:
The “fsetpos” function moves the read/write position of stream to the location indicated by the information in “pos”. The information in “pos” must have been obtained by a previous call to “fgetpos”.
Notes:
You cannot perform “fsetpos” operations on terminals or on SYSOUT.
“fsetpos” does not work on input streams which are concatenations of several input files.
See Also:
FGETPOS()
FGETPOS – get current read/write position.
(ANSI Standard)
Usage:
#include <stdio.h> stat = fgetpos(stream,pos);
Where:
- FILE *stream;
- indicates the stream whose position you wish to determine.
- fpos_t *pos;
- points to a location where “fgetpos” can store the position information.
- int stat;
- is zero if the read/write position is successfully determined, and non-zero otherwise.
Description:
The “fgetpos” function notes the current read/write position of stream and stores this information in the object indicated by “pos”. Information obtained through “fgetpos” can be used by the “fsetpos” function to return to this same position.
In the current implementation, “fgetpos” is just a dummy that always returns a failure status. It is included here for compatibility with the proposed ANSI standard.
