PDA

View Full Version : C++ and The File System



Mr.Macdonald
August 13th, 2008, 01:57 AM
I need a good guide/reference/tutorial on using C++ to deal with the file system, If one exists?!?

I need to know how to

read file names from a directory (ls)
create a new file without accidentally deleting an existing files contents (touch)
create a folder (mkdir)

hod139
August 13th, 2008, 03:26 AM
Boost filesystem is one option: http://www.boost.org/doc/libs/1_35_0/libs/filesystem/doc/index.htm

Mr.Macdonald
August 13th, 2008, 06:16 AM
I would prefer to not install boost

tuxerman
August 13th, 2008, 06:23 AM
The system() function can also be used for stray purposes.

system("mkdir newdir"); //for example

You'll have to include the stdlib header file.

Jessehk
August 13th, 2008, 06:28 AM
If you're on Ubuntu, an unportable way of doing it would be using the POSIX functions that come with glibc.
You can see the documentation for them here (http://www.gnu.org/software/libc/manual/html_node/File-System-Interface.html#File-System-Interface).

The Boost libraries are portable and would probably be a better choice.

nrs
August 13th, 2008, 06:37 AM
Looking around you get the impression people see Boost as a Meg.

LaRoza
August 13th, 2008, 08:30 AM
I need a good guide/reference/tutorial on using C++ to deal with the file system, If one exists?!?

I need to know how to

read file names from a directory (ls)
create a new file without accidentally deleting an existing files contents (touch)
create a folder (mkdir)


C has functions for those tasks, so C++ will have access to them. stdio.h and stdlib.h would have them, so start in the C++ versions of those.

For the second option, you'd have to test if a file exists then create it, not hard.

dwhitney67
August 13th, 2008, 02:07 PM
I would prefer to not install boost
Boost is your best choice if you prefer portability of your code. If you are more interested in the nuts-n-bolts of how things are done, then research the following C-library functions:

opendir()
readdir()
mkdir()
unlink()
stat()

For creating a file, use std ofstream, or fall back on the C-library's open() or fopen().

Avoid using the system() command unless you have a clear understanding of its limitations.

Mr.Macdonald
August 13th, 2008, 07:10 PM
I fixed the second thing i wanted, i just had a bug in my code!!

I will probably use boost, but is boost statically linked or dynamically?

If it is dynamically then that means that my user would need boost installed right?

I would prefer static, i think

scratch that i just learned that boost is header only, I love it!!

Zugzwang
August 14th, 2008, 10:37 AM
I will probably use boost, but is boost statically linked or dynamically?

If it is dynamically then that means that my user would need boost installed right?

[...]

scratch that i just learned that boost is header only, I love it!!

Not precisely true. There are parts that need to be linked. However, you *can* link it statically, if you want to.