Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

reading files

tieltiel Member Posts: 2
hello
I wrote a c++ program that takes files as input. the problem is that I have about 1000 files to input to the program and you bet that it's kind of hard to enter each filename manually. so I would like to save all the files in a directory and write a piece of code that automatically selects all the files in the directory, one after another, and input them in the program. Does someone know how to? I will definitely appreciate.

Comments

  • DB1DB1 Member Posts: 1,142
    : hello
    : I wrote a c++ program that takes files as input. the problem is that I have about 1000 files to input to the program and you bet that it's kind of hard to enter each filename manually. so I would like to save all the files in a directory and write a piece of code that automatically selects all the files in the directory, one after another, and input them in the program. Does someone know how to? I will definitely appreciate.
    :

    Just get yourself to the directory and use FindFirstFile() & FindNextFile() to step through the files, sending a pointer one at a time to your function.
    [CODE]
    void CLoopfilesDlg::OnButton1()
    {
    HANDLE dir;
    WIN32_FIND_DATA fd;
    char*fname;

    dir = FindFirstFile( "*.*", &fd );
    if( dir == NULL )
    {
    [GREEN]//
    // error_message: "Could not open current directory"
    //[/GREEN]
    exit( 1 );
    }
    [GREEN]//
    // Loop through all the files in the directory
    //[/GREEN]
    while ( 1 )
    {
    [GREEN]//
    // Make sure we are not passing a Directory name
    //[/GREEN]
    if( !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
    {
    [GREEN]//
    //Get the long file name
    //[/GREEN]
    fname = fd.cFileName;
    [GREEN]//
    // TODO: PassPointerToYourFunction(fname);
    //[/GREEN]
    }
    [GREEN]//
    // Get the next file
    //[/GREEN]
    if( !FindNextFile( dir, &fd ) )
    {
    break;
    }

    }
    FindClose( dir );
    }
    [/CODE]
  • tieltiel Member Posts: 2
    heloo DB1,
    this is just what I was looking for. Thanks a lot!
  • Andre YoungAndre Young USAMember Posts: 0

    ___ | http://forcoder.org | free video tutorials and ebooks about { Java, Delphi, C#, Visual Basic, C++, Objective-C, MATLAB, C, Scratch, PHP, JavaScript, Python, Assembly, Swift, PL/SQL, Perl, Ruby, Go, R, Visual Basic .NET ML, Julia, Prolog, VBScript, Hack, F#, Scheme, Rust, FoxPro, Ada, Clojure, Lua, Lisp, Kotlin, Fortran, SAS, D, Alice, Awk, Erlang, Scala, ABAP, Logo, COBOL, Transact-SQL, Bash, LabVIEW, Apex, Crystal, Dart }

Sign In or Register to comment.