Jump to content
  • Advertisement
Sign in to follow this  
DeadXorAlive

In C++, how to deal with files?

This topic is 4886 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

This is my question: (how) is it possible to do stuff with files like searching, reading the filenames into strings of a given or found directory, renaming, etc. (google didn't really help) So far I have covered the basic stuff in c++ like loops, classes, functions, pointers, arrays, overloading, etc. [working on inheritance and polymorphism now). I also know how to use (the basics of) the fstream class, but I don't see such aforementioned functionality in it. I'm using Dev-C++, and code in console-mode. Would I have to use some standard C library? Is there a way to use system to call DOS commands in any meaningful way? (I know DOS) I know one could use the windows API but that's way beyond me as of yet! Or should I just forget it at this stage? I'm trying to practice what I've learnt by coding some 'usefull' (to me) apps before going into the graphics part.

Share this post


Link to post
Share on other sites
Advertisement
fstream just deals with one file.

For directory listings and things like that, you'll likely need to use the windows API or C-style directory code [dir.h? it's been like 5 years...]. Technically system could probably work, but doing that is decidedly bad [what happens if someone replaces dir with "delete all my files"?] And generally, you might want to wait on stuff like that for a little bit.

Share this post


Link to post
Share on other sites
Ok, thanx a lot.
Maybe i'll just give boost a try when i'm getting better. It looks goods.

I guess I'm too much familiar with DOS to think that file management can be difficult. Or not familiar enough with programming.

Share this post


Link to post
Share on other sites
I've personally just stuck with the Win32 API for that stuff.

Here is MSDN's list of relevant functions. Typically, you'll use FindFirstFile(), FindNext(), and FindClose() to get a list of files/directories under a certain file. MoveFile() is used for both moving and renaming a file. And a bunch of others exist for obvious tasks, such as DeleteFile(), CopyFile(), etcetera.

Share this post


Link to post
Share on other sites
You could use the FindFirstFile, FindNextFile and FindClose routines in the WIN32 API.


void get_all_files(char* lpszDirectory)
{
WIN32_FIND_DATA data;
HANDLE hFound = NULL;
if((hFound = FindFirstFile(lpszDirectory,&data)))
{
while(FindNextFile(hFound,&data))
{
cout << data.cFileName << "\n";
}
}
FindClose(hFound);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAlive
I know one could use the windows API but that's way beyond me as of yet!



I doubt it. The online MSDN documentation is excellent, and in many cases the functions are very easy to use. Now don't get me wrong, some of the Win32 stuff is sickeningly difficult, but just take a look at the definitions for MoveFile, CopyFile, CreateFile, DeleteFile, etc and you'll see what I mean...it's easy.

Share this post


Link to post
Share on other sites
Sweet, I actually get these win32 api functions to work, they aren't difficult at all to get going. Thnx alot again.

As a total win32api newbie, am I correct in this assumption (I understand how the other stuff works by reading the msdn site) regarding Brocketino's code:

HANDLE hFound = NULL;
This (I mean the HANDLE) is some typedef, for communication between the os and your prog or something like that. It is a bool? integer? it doesn't really matter because it's all typedefs anyway?

hFound = FindFirstFile(lpszDirectory,&data);
Here the FindFirstFile function returns a value which is used to check if it was succesful and some other things, so FindNextFile can do it's job: it has a search handle, whatever that is. I guess it probably isn't a boolean value because you want more information.

Anyway, it works. Just curious how it works.

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAlive
As a total win32api newbie, am I correct in this assumption (I understand how the other stuff works by reading the msdn site) regarding Brocketino's code:

HANDLE hFound = NULL;
This (I mean the HANDLE) is some typedef, for communication between the os and your prog or something like that. It is a bool? integer? it doesn't really matter because it's all typedefs anyway?

hFound = FindFirstFile(lpszDirectory,&data);
Here the FindFirstFile function returns a value which is used to check if it was succesful and some other things, so FindNextFile can do it's job: it has a search handle, whatever that is. I guess it probably isn't a boolean value because you want more information.

Anyway, it works. Just curious how it works.

Win32 typedefs a lot of things. A HANDLE is really an interger, and it is used to refer to a particular instance of something, like a pointer to a variable does. You don't need to worry about its value (unless you need to check if its NULL for errors or something), you just need to pass it to Windows so it knows which file or whatever you are talking about.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!