c++ fileoperations under linux

Started by
6 comments, last by genne 21 years, 6 months ago
Hi!! Anyone who knows how to operate on files under linux using c++??.. I would like to be able to list files/directories under a specified folder in the filesystem, and copy/move/remove files as well. Any ideas out there?? Thx for any help! [edited by - genne on October 20, 2002 11:38:24 AM]
Advertisement
try "readdir()" for reading directory structure.... it''s similar to "findfirst" and "findnext" combination except that it reads only the directories. Look up the manual for detail....
Z
Don''t have any manual, and i''ve searched the internet without any luck =(

But thx for your tip anyway! I''ll check that out!
Consider searching Google for examples of directory operation using C++ under Linux.

Kuphryn
quote:Original post by genne
Don't have any manual

You should. Open a terminal and type 'man some-function-name ' and if you have a manpage for it installed, it will show it to you.

quote:Original post by genne
I would like to be able to list files/directories under a specified folder in the filesystem...

As has been said: opendir, readdir, closedir.

quote:Original post by genne
... and copy/move/remove files as well. Any ideas out there??

Copying files efficiently takes a little work, not too much though.

Copying files: First try to 'fake copy' the file, since it would be fastest, with 'link'. If link fails, check what error occured (errno in errno.h would be set). If the error is EXDEV, EPERM, EMLINK, or ELOOP, manually copy the file (with open, read, write, close; or with fopen, fread, fwrite, fclose; or with fstream). If the error is EEXIST and you want to overwrite the file, then overwrite it. Otherwise, the error isn't something that we can work around like that.

Moving files: C has a 'rename' function, if you want to use it. It will overwrite the file. Otherwise, do the same thing as you would to copy the file, except delete the old copy afterwards if everything goes well, with:

Deleting files: 'remove' (part of C) or 'unlink' (part of POSIX, among other things)

There's an example of all of this in CPT in src/unix/system.c, if you're interested.



[edited by - Null and Void on October 21, 2002 5:28:04 AM]
Humor in the manpages...wow....

I swiveled over to my linux machine when I saw readdir mentioned, and typed in man readdir

First sentence in the description: This is not the function you are interested in.

lol....
quote:Original post by genne
Don''t have any manual, and i''ve searched the internet without any luck =(

But thx for your tip anyway! I''ll check that out!


In that case, you didn''t really search. Look for "File IO" using www.google.com/linux

Don''t forget the /linux...

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com]
[Cyberdrek | ]
Aah! Thx everybody! Didn''t know about the "man function-name" possibility, linux is full of surprises =)

Now i just have to make it work for windows as well =(... but thats an other story!

This topic is closed to new replies.

Advertisement