Sign in to follow this  

winapi - reading files names

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well... you can always use the good and old C function


#include <stdio.h>
#include <windows.h>

void main() {
FILE* fp = fopen("some_path", "authorize(diff stuff)");
if(fp != NULL) {
char[256] text;
fscanf(fp, "%s", text);
MessageBox(NULL, text, "Test", MB_OK);
}

if(fp != null)
fclose(fp);
}



that should do the trick, read about the functions they are easy...
Good luck.

Share this post


Link to post
Share on other sites
See the Find* series of functions Here for searching for *.txt files on a Win32 platform.

I'd recommend not using fopen unless you're really using C - but even then I wouldn't recommend it, because I don't recommend C :-P.

If you're using C++, iostreams are likely a better alternative. The following is equivilant to Ancient Spirit's example (note that main must return int, returning void is not proper C++)

#include <iostream>
#include <string>
#include <windows.h>

int main() {
std::ifstream file( "some_path );
if ( file.good() ) {
std::string text;
//to get one word (equivilant):
file >> text;
//to get one line:
//std::getline( file , text );

MessageBox( NULL , text.c_str() , "Test" , MB_OK );
}
if ( file.is_open() ) file.close(); //this is automatic when file is destroyed, only included to show a more direct alternative
}



Ancient Spirit's code will have a buffer overflow if the first "word" of the file is greater than 255 characters. std::string is a dynamic string, and does not suffer from such a security hole/bug. The ease of doing what AS just did is one of the many reasons I'm recommending an alternative.

Share this post


Link to post
Share on other sites
i think i badly formulated my question :)
i wanted to get THE LIST of *.txt files on C:, not their contain :)
Prototype understood me well :)
i will read about Find(First/Next)File
thx

btw: i've been using C, not C++

// edit - i've found a resolution of my problem - it's here

Share this post


Link to post
Share on other sites
Sign in to follow this