In C++, how to deal with files?

Started by
7 comments, last by Rebooted 18 years, 9 months ago
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.
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.
The boost filesystem library lets you do such things using cross platform code. The boost libraries may be a bit too advanced for you at the moment though.
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.
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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);}
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.

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.
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.

This topic is closed to new replies.

Advertisement