Hello, I've been trying to get a list of files from given directory in C++ (with win32)
but i encountered some weird problems -
//test snippet
-
#include <windows.h>
-
#include <stdio.h>
-
#include <iostream>
-
-
int main()
-
{
-
-
//find file data
-
WIN32_FIND_DATA findFileData;
-
HANDLE hFind = FindFirstFile((LPCSTR)"C:\\Documents and
-
Settings\\Me\\Desktop\\Pictures", &findFileData);
-
-
-
//print it out
-
std::cout <<"Files in dir:" <<std::endl;
-
std::cout <<(char*)&findFileData.cFileName<<std::endl; //problem might be here
-
-
-
char pause;
-
std::cin>> pause;
-
return 0;
-
}
-
my output is:
and I'm certain that it's a folder, not a file...
even when I do - FindNextFile (hFind, &findFileData)
I'm still getting "Pictures"!
anything wrong with the code?
why am i not getting an actual file name?
wondering if it's my WCHAR to char* cast...(ugly isnt it?)
P.S.
is there a quick way to find the number of files in a directory?
(I am using VC++ 2008 with Windows XP Pro)
Thank you in advance,
Netwatcher.
20 47695
So, i got that all good and figured out :p -
//this goes to superduperwhat.cpp
-
#include <windows.h>
-
#include <stdio.h>
-
#include <iostream>
-
char* locjpg = "C:\\Documents and Settings\\yair\\Desktop\\UNIMPICS\\*.jpg";
-
char* locbmp = "C:\\Documents and Settings\\yair\\Desktop\\UNIMPICS\\*.bmp";
-
char* arr[50];
-
char* nfilen;
-
-
-
int main()
-
{
-
char desOfile[80]; //destination of file, load to a LPDIRECT3DTEXTURE9
-
WIN32_FIND_DATA findFileData;
-
HANDLE hFind = FindFirstFile((LPCSTR)locjpg, &findFileData);
-
std::cout <<"Picture files in dir:" <<"\n"<<locjpg<<"\n"<<locbmp<<"\n\n"<<std::endl;
-
std::cout <<findFileData.cFileName<<"\n"<<std::endl;
-
nfilen = findFileData.cFileName;
-
strcpy(desOfile,"C:\\Documents and Settings\\yair\\Desktop\\UNIMPICS\\");
-
strcat(desOfile,nfilen);
-
-
-
//change to something to navigate between images, add bmp for search
-
for (unsigned f=0;f<20;f++) //change
-
{
-
//tomalloc
-
FindNextFile(hFind,&findFileData);
-
arr[f] = findFileData.cFileName;
-
strcpy(desOfile,"C:\\Documents and Settings\\yair\\Desktop \\UNIMPICS\\");
-
strcat(desOfile,nfilen);
-
std::cout <<arr[f]<<std::endl;
-
std::cout<<desOfile<<"\n\n"<<std::endl;
-
/*arr[f]=desOfile; for actual app IMPORTANT*/
-
-
-
}
-
-
-
-
-
-
char pause;
-
std::cin>> pause;
-
return 0;
-
}
-
O.K. so everything is nice and happy until the for loop, how do i know how many files are in the directory so i can know how many times to loop? and maybe how many chars i need for "arr".
Was that a squirrl?
Netwatcher.
You stay in the loop until FindNextFile returns FALSE. The call GetLastError and look for ERROR_NO_MORE_FILES.
If you counted the number of times FindNextFile returned TRUE, then you have the number of files.
Be sure to read the MSDN page for FindNextFile.
Umm.... it just returns the last file it finds(the same file again and again) until i tell it to stop!
Edit: HAHA no need to actually check for the error but thx for pointing my attention at the fact that it returns something if it fails... forgot about that(yep, stupidity gets over me sometimes, well really often) -
while(FindNextFile(hFind,&findFileData))
-
{
-
-
f++;
-
arr[f] = findFileData.cFileName;
-
strcpy(desOfile,"C:\\Documents and Settings\\yair\\Desktop\\UNIMPICS\\");
-
strcat(desOfile,nfilen);
-
std::cout <<arr[f]<<std::endl;
-
std::cout<<desOfile<<"\n\n"<<std::endl;
-
-
-
}
-
this looks much more neat then the last one and works well.
not paying attention to details gets me into panic
for the Left4Dead players:
"director_panic_forever 1;director_force_panic_event 1;z_spawn witch;z_spawn tank"
bind that with the quotes to any key and watch ur friends run away in fear...
Banfa 9,065
Expert Mod 8TB
I don't know what you think this -
arr[f] = findFileData.cFileName;
-
does but probably not what you think. arr ends up as an array of pointers all pointing to the same buffer so all point to the same data, the last file you found.
*headscratch...
so what do i need to do in order to save this the right way?
its not directed to the last file i found its directed to nothing, say i have exceeded the length of the streamsize when i try to print out arr[element]
Edit:
my right- to actually store the letters inside the (char*) in the spacific element of the array! (it is a char* array)
cant find a str method for this >,< or is there?
so what do i need to do to findFileData?
well think i made my work difficult cos arr is an array of pointers >,<
how else can i store whole strings in a non-pointer way?
vector<string>
This situation just calls for it.
If you use Unicode compiler settings replace string with wstring. - #include <vector>
-
#include <string>
-
-
using namespace std;
-
-
-
vector<string> vfList;
-
vfList.push_back("file1.txt");
-
vfList.push_back("file2.txt");
-
// keep pushing file names as long as you want
-
-
vector<string>::iterator itr;
-
for(itr = vfList.begin(); itr != vfList.end(); itr++)
-
{
-
// Do actions with each file.
-
// Use itr->c_str() to get the string buffer
-
// or *itr to get the actual C++ string object.
-
}
Now, I'm not really sure why you use the raw Windows API, but I'd suggest scraping this whole thing and using Boost.Filesystem instead.
The Win32 API was designed for C programmers some 15+ years ago. Even Microsoft doesn't want it anymore (see .NET).
Thanks!
using multi-byte charset, :p this vector thing is new to me gonna read on that some
vector doesnt seem to have this method.
do i need to a include a .lib or do anything special before using vectors?
some time later in the day>>
Darnit! its a '::' method (I'm tired...) was just playing around and found it.
If the world is crashing down on you,
check for missing semicolons.
Netwatcher.
Damn i didnt notice u using the namespace...
I was so frustrated why it won't work when i just do "vector<string>"...
or do i need to
the while loop goes through files in the directory
the "findFileData.cFileName" is a CHAR(WCHAR in unicode)
do i just use push back in the while loop?
does that create a new element or creates and stuffs a new element? - int main()
-
{
-
-
char desOfile[80];
-
std::vector<string> victor;
-
std::vector<string>::iterator itr;
-
-
WIN32_FIND_DATA findFileData;
-
HANDLE hFind = FindFirstFile((LPCSTR)locjpg, &findFileData);
-
std::cout <<"Picture files in dir:" <<"\n"<<locjpg<<"\n"<<locbmp<<"\n\n"<<std::endl;
-
std::cout <<findFileData.cFileName<<"\n"<<std::endl;
-
nfilen = findFileData.cFileName;
-
strcpy(desOfile,"C:\\UNIMPICS\\");
-
strcat(desOfile,nfilen);
-
-
-
-
for(itr = victor.begin();itr!=victor.end();itr++)
-
{
-
victor.push_back(findFileData.cFileName);
-
strcpy(desOfile,"C:\\UNIMPICS\\");
-
strcat(desOfile,"\\");
-
strcat(desOfile,nfilen);
-
std::cout<<desOfile<<"\n\n"<<std::endl;
-
-
-
-
}
-
say i have that, what does <string> should be?
"oh i forgot u used the std..."
Using STD in your source code just doesn't sound right anymore...
Netwatcher.
Damn me... forgot u used the namespace again... (string)
nyway...
after a nightmare of trial and error and reading MSDN...
i got it... i finally got it :D
got it all figured out and i probably should stop writing everything I'm panicing with... (see silly posts above)
Thank you so much.
I'm not gonna use arrays again!!! (wait what? just kidding)
the iterator doesnt appear to have any method called c_str though.
for the sake of knowledge, here is the complete, functional while loop -
while(FindNextFile(hFind,&findFileData))
-
{
-
int temp;
-
victor.push_back(findFileData.cFileName);
-
strcpy(desOfile,"C:\\UNIMPICS\\");
-
strcat(desOfile,nfilen);
-
cout<<desOfile<<"\n\n"<<endl;
-
cout<<victor[f]<<endl;
-
f++;
-
}
-
}
-
Everybody's got a brain,
learn how to hack it, and the world is yours!
Netwatcher.
@unauthorized
What is this **** ?!!!!
Under Windows, you must use Win32 api. Period.
All OS from Win 95 to Windows 7 are written in C, C++, ASM Win32, with the Win32 api
All Windows softwares are written with Win32 api (MFC is a wrapper)
All Windows games are written with Win32 api (DirectX and OpenGL)
Incredible to read such things...
Banfa 9,065
Expert Mod 8TB @george666
This is partly a matter of semantics, if a wrapper you use then uses the WIN32 API you are still not using it and do (should) not really need to know anything about it only about the interface the wrapper you are using defines.
Using WIN32 directly is definitely not what Microsoft want you to do nor for that matter do they want you using MFC. Both of these are out-dated forums of writing programs for Windows, although I imagine they are both still used.
The prefered method for creating Windows application is now .NET. It would not be unreasonable to assume that the Windows implementation of the .NET Framework makes some use of the WIN32 interface when implemented on the Windows platform, however Windows is not the only platform that the .NET framework has been implemented on so using .NET is not implicitly using WIN32.
In a complex system like Windows you use the interface of your choice and you do not worry too much about what interfaces that interface uses.
So you blanket statement that all Windows development uses the WIN32 API make be technically correct if you delve down through the layers of code deeply enough but it is not the environment that most Windows application programmers use and for straight programming it is certainly not in very common usage any more.
Your view is out-dated and presented in a belligerent manner and not that useful.
Is there a way to solve my problem without using a template (for educational purpose)?
The rawer the better(no assembly code lol),
Even only general guidelines on how to solve this.
Banfa 9,065
Expert Mod 8TB
By without a template do you mean without using vector and string?
Yep!
Without using STL or any other templates.
Banfa 9,065
Expert Mod 8TB
I can think of 2 ways either use a dynamically allocated array of pointers.
That is you allocate and array of pointers to char (say 10 to start off with). Each time you read a file name you allocate memory to hold that file name and assign it to the next pointer in the array. When you reach the 11th file then you reallocate you array of 10 pointers to be 16 pointers (I believe 1.6 is an efficient growth factor). You will also have to keep a count of how many file-names you have read and make sure you deallocate the memory correctly.
You will need to use the function malloc, realloc and free for memory handling.
The second option is a linked list (look it up on wikipedia). I linked list can just be grown as you read more file names, you just need adding nodes to the list. Again you will need to use malloc and free (but not realloc).
Think a linked list is the way to go on this one.
(In these situation python is so tempting)
Thanks,
Netwatcher. http://www.csref.info <-> code reference site.
Used an allocated array but with new(C++) instead of malloc(C)
Tried malloc too, found that new is simply faster (though delete is slower than free, and there's no reallocation with new :p) -
-
char** pt;
-
pt = new char*[50];
-
unsigned n=0;
-
for(/*noimportant*/;n++)
-
{
-
/*lala
-
lala*/
-
pt[n]= hFindFile.cFileName;
-
}
-
for (n=0;pt[n]!=NULL;n++)
-
{
-
cout <<pt[n]<<', ';
-
}
-
delete [] pt;
-
return 0;
-
}
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hamed |
last post by:
Dear Folks
I am programming a page that asks user a directory name and lists files
of type *.xls in the directory in client's machine. He/She will then
select some files to upload them to the...
|
by: Mary Jane Pronio |
last post by:
What I want to do is take that output, use the data within it to put
into a shell script as input to another installation of DB2 Connect on
another AIX box. Any ideas about how to do this?
|
by: RML |
last post by:
hey guys,
i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to...
|
by: Don |
last post by:
I intermittently get a runtime Compilation Error that says 'The
compiler failed with error code 2000'. It appears that a DLL cannot
be found in the 'temporary asp.net files' directory.
The...
|
by: Shawn Mehaffie |
last post by:
I have an application I want to be able to:
1) Store user specifc settings in ther "My Documents".
2) Store some information in "All Users" document directory.
3) I also want to be able to...
|
by: Kristan |
last post by:
Hi there, quick question, how would I retrieve a list of files in ANSI C in
a purely platform independent way?
Any pointers would be great!
thanks
Kristan
|
by: dcruncher4 |
last post by:
DB2 8.2.3
is there a sql equivalent to get the information provided by
db2 list node directory
db2 list db directory.
thanks.
|
by: mmohanra |
last post by:
I am trying to list files in a folder in a remote server. I am using Scripting.FileSystemObject. This seems to be really slow. After a long time the connection times out. Is there any other way to...
|
by: froditus |
last post by:
Hello everyone,
is it possible to list files from directory other than in apache web
directory?
my web folder is placed on c:/ and i put my files in windows directory
"d:/files/images/".
I...
|
by: CSS Newbie |
last post by:
So I want to use a javascript photo gallery widget I found...but it requires me to enter a comma delimited list of all the photo files I want to use.
I want to show all the picture files within...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |