Otwieranie plikow z folderu
Ostatnio zmodyfikowano 2011-04-22 16:29
diego997 Temat założony przez niniejszego użytkownika |
Otwieranie plikow z folderu » 2011-04-21 19:17:24 Ostatnie pytanie
Jak zrobić żeby program otwierał pliki tekstowe które są umieszczone w konkretnym folderze? I czy to również sprowadza się do tego że trzeba utworzyć pętlę for która sprawdza literka po literce czy dany plik istnieje ?? |
|
pekfos |
» 2011-04-21 19:27:14 io.h lub dirent.h . użyj google lub wyszukiwarki na stronie |
|
yoogi |
» 2011-04-21 19:27:52 Masz do dyspozycji 2 biblioteki. PIerwsza to "io.h", a druga to WinAPI. Osobiście korzystałem z biblioteki "dirent.h" dirent.h
#ifndef DIRENT_H #define DIRENT_H
#include <windows.h> #include <string.h> #include <assert.h>
typedef struct dirent { char d_name[ MAX_PATH + 1 ]; WIN32_FIND_DATAA data; } dirent;
typedef struct DIR { dirent current; int cached; HANDLE search_handle; char patt[ MAX_PATH + 3 ]; } DIR;
static DIR * opendir( const char * dirname ); static struct dirent * readdir( DIR * dirp ); static int closedir( DIR * dirp ); static void rewinddir( DIR * dirp );
#if defined(_MSC_VER) && _MSC_VER >= 1400 # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE) #else # define STRNCPY(dest,src,size) strncpy((dest),(src),(size)) #endif
static DIR * opendir( const char * dirname ) { DIR * dirp; assert( dirname != NULL ); assert( strlen( dirname ) < MAX_PATH ); dirp =( DIR * ) malloc( sizeof( struct DIR ) ); if( dirp != NULL ) { char * p; STRNCPY( dirp->patt, dirname, sizeof( dirp->patt ) ); dirp->patt[ MAX_PATH ] = '\0'; p = strchr( dirp->patt, '\0' ); if( dirp->patt < p && *( p - 1 ) != '\\' && *( p - 1 ) != ':' ) { * p++ = '\\'; } * p++ = '*'; * p = '\0'; dirp->search_handle = FindFirstFileA( dirp->patt, & dirp->current.data ); if( dirp->search_handle == INVALID_HANDLE_VALUE ) { free( dirp ); return NULL; } dirp->cached = 1; } return dirp; }
static struct dirent * readdir( DIR * dirp ) { assert( dirp != NULL ); if( dirp->search_handle == INVALID_HANDLE_VALUE ) { return NULL; } if( dirp->cached != 0 ) { dirp->cached = 0; } else { if( FindNextFileA( dirp->search_handle, & dirp->current.data ) == FALSE ) { FindClose( dirp->search_handle ); dirp->search_handle = INVALID_HANDLE_VALUE; return NULL; } } STRNCPY( dirp->current.d_name, dirp->current.data.cFileName, sizeof( dirp->current.d_name ) ); dirp->current.d_name[ MAX_PATH ] = '\0'; return & dirp->current; }
static int closedir( DIR * dirp ) { assert( dirp != NULL ); if( dirp->search_handle != INVALID_HANDLE_VALUE ) { FindClose( dirp->search_handle ); dirp->search_handle = INVALID_HANDLE_VALUE; } free( dirp ); return 0; }
static void rewinddir( DIR * dirp ) { if( dirp->search_handle != INVALID_HANDLE_VALUE ) { FindClose( dirp->search_handle ); dirp->search_handle = INVALID_HANDLE_VALUE; } dirp->search_handle = FindFirstFileA( dirp->patt, & dirp->current.data ); if( dirp->search_handle == INVALID_HANDLE_VALUE ) { free( dirp ); return; } dirp->cached = 1; }
#endif
A tu masz przykładowy program: #include <stdio.h> #include "dirent.h" #include <vector> int main() { DIR * dp; struct dirent * ep; for(;; ) { dp = opendir( "c:/" ); if( dp != NULL ) { Sleep( 500 ); system( "cls" ); while( ep = readdir( dp ) ) { printf( ep->d_name ); printf( "\n" ); } closedir( dp ); } else { perror( "Nie mozna otworzyc katalogu" ); } } getchar(); }
|
|
pekfos |
» 2011-04-21 19:41:08 po co dajesz kod dirent.h? jest nadal nawet z najnowszymi kompilatorami. wywal ten kod, nie ma co serwera zawalać ;P |
|
diego997 Temat założony przez niniejszego użytkownika |
» 2011-04-21 20:19:39 znalazłem tutorial odnośnie tej biblioteki io.h, no ale maxymalnie moge wyszukać dwa pliki przez findfirst i findnext a jak znaleść 3 4 i więcej ? |
|
malan |
» 2011-04-21 20:20:50 Gdzie to wyczytałeś? Bzdura... |
|
diego997 Temat założony przez niniejszego użytkownika |
» 2011-04-21 20:23:46 http://cpp0x.pl/forum/temat/?id=447 |
|
yoogi |
» 2011-04-21 20:40:37 Akurat w Visual Studio 2010 Ultimate zaraz po instalacji nie ma tej biblioteki. |
|
« 1 » 2 3 |