handeling files etc in c++

Started by
4 comments, last by Sync Views 16 years, 1 month ago
How do I do stuff with the filesystem in c++? eg copy files, move files, check if a file exists, get a list of files in a directory, find the temp directory, find the working directory, fiond my documents etc? I'm sure one of the many headers that came with vs2008 has basic functions like that but I can't seem to find it...
Advertisement
If you're working on Windows, either resort to the Win32 API or .NET Framework.
C++ itself has not concept of directories. Either use OS specific resources or something like boost::filesystem.
You can use files in your program using the I/0 system, the fstream library will give you the ability to read and write files.
The last post was correct you can use the std::fstream along with the header file of #include <fstream> to access and write files that are and are not in your programs directory however you need to know where those files are.

If you want the user to be able to pick one there is a win32 api that useing a dialog box to let the user select a file.

[source lang"cpp"]// a quick example#include <fstream>int main(){std::fstream File;File.open("Test.txt",std::ios::in); //use std::ios::out to do ouputing to filesif(!File){std::cout<<"Error no file to load\n"; return 0;}char Buffer[250];File >>Buffer;std::cout <<Buffer;File.close();return 0;}


Ok thats basicaly how to read from a file one line at a time this code snippet only reads one line google fstream and you will learn more now you will not that most pepole would say i have a bug cause i use char but to use the >> operators it only returns to a char arry for the most part.

however doing useing char can be dangoures because of buffer overflows so check out about useing fstream with std::string

Regards Joue.
Now ive got my grafics sorted and I went to do this today. I want to use the boost filesystem but ran into a few problems.

It would semm boost doesn't support visual studio 2008. I went through some of the files to make it see "MSC_VER 1500" as a valid compiler version but now it just says "LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc80-mt-gd-1_34_1.lib'" instead... Am I just going to wait untill they update boost to work for the 2008 compiler? (9.0?)

This topic is closed to new replies.

Advertisement