Sign in to follow this  
kingpinzs

How to search curent directory for txt file

Recommended Posts

How would I make a function to look for any files that start with the word image and load all text files that start with that name? This is so I can load my info about every image example image file name image width image height image transparent color this will be saved for every single image so I dont have to type it in every time for every image I use.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
for some reason MS examplse for FindFirstFile and FindNextFile are not working they just crash on me. any onw know why?


They work find for me plug and play.


#define _WIN32_WINNT 0x0400

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

const char* fileName = "YourFile.Ext";

int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

printf ("Target file is %s.\n", fileName );
hFind = FindFirstFile(fileName , &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
return (0);
}
else
{
printf ("The first file found is %s\n",
FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}



Just type in the file name you are looking for in the variable fileName and it should work.

Share this post


Link to post
Share on other sites
only one question how would i find all files with name image*.txt
so it would load like 10 files that start with image and anything after and there all txt files

Share this post


Link to post
Share on other sites
you can use this:

#include <stdio.h>
#include <io.h>

int main(int argc,char *argv[])
{
char **flist = findfiles(argv[1]);
char **s = flist;

while (*s) {
printf("%s\n",*s);
s++;
}
free(flist);
}


or that:


#include <stdio.h>
#include <io.h>

int main(int argc,char *argv[])
{
struct _finddata_t c_file;
long File;

if((File = _findfirst( "*.c", &c_file )) == -1L)
printf("No %s files in current directory!\n", argv[1]);
else
{
printf("%s\n", c_file.name);

while( _findnext( hFile, &c_file ) == 0 )
{
printf("%s\n", c_file.name);
}
_findclose(File);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by bjogio
you can use this:
*** Source Snippet Removed ***
or that:
*** Source Snippet Removed ***


it worked perfectly exactley what I needed thanks so much.

Thanks for every ones help it lead me in the roght direction and helped me learn alot I thank you very much

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this