• Advertisement

# Directories and File Manipulation

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

Hi im very new to GameDev.Net so i hope i don't sound too stupid when i ask extreme beginner questions. I have been using VB 6.0 for a few years now and C++ is proving to be a larger challenge than expected. I have search google to the best of my ability and was unable to find the answer to a few basic question (at least without getting so confused by advanced code surrounding my answer). ------------------------------------------------------------------------------- Topic #1: Directories How do i scan a directory for it's contents? How can i find the dir location of the app that im running? any various other information about directories and/or directory manipulation would be appreciated. ------------------------------------------------------------------------------- Topic #2: Files How do i use the basic manipulation functions for files? ie. Creating, Deleting, Renaming, Writing, Reading, Appending, etc. Once again any and all addictional information surrounding file manipulation would be appreciated. ------------------------------------------------------------------------------- NOTE: MY CURRENT LEARNING style HAS BEEN FROM SEARCHING THROUGH SAMPLE CODE I WOULD MUCH APPRECIATE IT IF WHEN FUNCTIONS OR CALLS ARE SPOKE OF THE SYNTEX AND THE WAY IT IS USED IN CODE WOULD BE GIVEN ALSO. Thank you very much. Lordoftools

#### Share this post

##### Share on other sites
Advertisement
C++ had no native functions for scanning a directory. You either need to use platform specific functions or a library like boost::filesystem.

The complete path to the application is usually passed as argv[0].

Some file system functionality is again, not supplied by C++ such as deleting or renaming files. Again you need to use platform specific functions or something like boost::filesystem.

For opening, reading and writing files see the std::fstream class in your compiler's documentation.

#### Share this post

##### Share on other sites
for my purposes my applications will only need to be run by windows users, i'd prefer to use universal standards but if this is not possible then window specific ones are acceptable. I could even bare with methods that only work for windows 2000 and newer. I will look into the boost::filesystem which you have suggested but i'd appreciate further ideas on how to handle these issues, so that i may choice the one/ones best suited for my needs.

Thank you
Lordoftools

#### Share this post

##### Share on other sites
WOW, i just spent an hour going through the boost::filesystem that you suggested and it looks GREAT but the build instructions are fragmented over multiple web sites and i find myself continuously using my back button and reclicking links multiple times just to understand all that im reading about BJam, how to build it, how to use it to build the rest of Boost::Filesystems and this is all long before i can even touch the coding which i'll need to learn inorder to use the system.

Could someone please help simplify the extremely detailed and overly fragmented instructions on how to get Boost installed on Visual C++ 6.0 so that i may work on the coding itself and maybe once i understand more give this install another go on my own?

#### Share this post

##### Share on other sites
Since you're using MSVC++, you can also go with the Win32 API functions. Search msdn.com for these keywords. They all come with half-decent source code examples:

1a) FindFirstFile, FindNextFile to enumerate directory contents.

1b) GetModulePath -- this one's special, because you will most definitely want to use the Unicode version (the ASCII version is limited to a very short directory path length)

The following code can be used to get the application's directory:
#include <string>using std::string;using std::wstring;#include <windows.h>// use Unicode (wide) version of GetModuleFileNamewstring get_module_path_wide(void){	wchar_t wide_buf[65536];	wstring module_name;	if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))		return module_name;	wide_buf[65535] = L'\0';	module_name = wide_buf;	size_t pos = module_name.find_last_of(L'\\');	if(pos != string::npos)		module_name = module_name.substr(0, pos);	return module_name;}// use this function to convert from wide wstring to ASCII string if need bestring wsts(const wstring &src_string){	size_t src_len = src_string.length();	if(src_len == 0)		return "";	char *buf = new(std::nothrow) char[src_len + 1];	if(buf == 0)		return "";	wcstombs(buf, src_string.c_str(), src_len);	buf[src_len] = '\0';	string final_string = buf;	if(buf != 0)		delete [] buf;	return final_string;}

2) There are tons of Win32 functions for this, but everything you need is included with the C++ language itself.

http://www.cplusplus.com/doc/tutorial/tut6-1.html is a good starter. It will at least show you all of the classes and parameters you need to know to get started. From there, you can google or msdn for more info (MSVC++ also comes with a locally installed C++ language reference).

#### Share this post

##### Share on other sites
Simplified instructions on getting boost::filesystem to work with your compiler: Unzip the boost library to a directory. Add the boost_1_32_0 directory to your project's include path. Add the files in boost_1_32_0/libs/filesystem/src to your project. Edit boost_1_32_0/boost/config/user.hpp and uncomment the line #define BOOST_ALL_NO_LIB.

#### Share this post

##### Share on other sites
Thank you both for your help.

Taby, i read somewhere that string.h is not a header file which MSVC++ accepts. I have even tried to include this file in my apps and it gave me file not found errors. Do i have to do something special to get this included so that your source code will work for me or maybe do i just need to DL it from somewhere because it may not come with MSVC++?

-Lordoftools

#### Share this post

##### Share on other sites
Was testing your code Taby and managed to get rid of the string.h errors somehow but im stillf having problems here's the code then the errors.

#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream.h>
using std::string;
using std::wstring;

wstring get_module_path_wide(void);
string wsts(const wstring &src_string);

int main(int argc, char* argv[])
{
wstring Path;

Path=wsts(get_module_path_wide());

cout<<Path;
cin.ignore;

return 0;
}

// use Unicode (wide) version of GetModuleFileName
wstring get_module_path_wide(void)
{
wchar_t wide_buf[65536];
wstring module_name;

if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
return module_name;

wide_buf[65535] = L'\0';

module_name = wide_buf;

size_t pos = module_name.find_last_of(L'\\');

if(pos != string::npos)
module_name = module_name.substr(0, pos);

return module_name;
}

// use this function to convert from wide wstring to ASCII string if need be
string wsts(const wstring &src_string)
{
size_t src_len = src_string.length();

if(src_len == 0)
return "";

char *buf = new(std::nothrow) char[src_len + 1];

if(buf == 0)
return "";

wcstombs(buf, src_string.c_str(), src_len);
buf[src_len] = '\0';

string final_string = buf;

if(buf != 0)
delete [] buf;

return final_string;
}

ERRORS:
--------------------Configuration: App_Path - Win32 Debug--------------------
Compiling...
App_Path.cpp
C:\Documents and Settings\Derik\Desktop\Programming\C++\App_Path\App_Path.cpp(18) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
C:\Documents and Settings\Derik\Desktop\Programming\C++\App_Path\App_Path.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<unsigned short,struct std::char_traits<unsigned shor
t>,class std::allocator<unsigned short> >' (or there is no acceptable conversion)
C:\Documents and Settings\Derik\Desktop\Programming\C++\App_Path\App_Path.cpp(21) : warning C4551: function call missing argument list
Error executing cl.exe.

App_Path.exe - 2 error(s), 1 warning(s)

#### Share this post

##### Share on other sites
Your problem seems that you are trying to use std::wstring with std::cout. std::cout uses narrow characters, and std::wstring is wide characters. Instead try using std::wcout.

#### Share this post

##### Share on other sites
You're also using <iostream.h> instead of <iostream>. The latter is standard; the former is unspecified by the standard and therefore unpredictable.

#### Share this post

##### Share on other sites
i thought of that already, first off with <iostream.h> the functions have proven to work teh same with or without the .h but if <iostream> is the standard i shall use that incase it's "unpredictability" proves to be an issue later on.

second, you said that std::cout is using narrow characters while get_module_path_wide is useing wide characters but i was under the impression that the function wsts() was there to convert the wide characters into the narrow which is why i have it in there as Path=wsts(get_module_path_wide()); instead of Path=get_module_path_wide();

Am i wrong to assume that this should cause a successful convertion and if so will the variable 'Path' still hold the string or is it simply cout can't display it?

I will be needing the path of the application in variable form anyways the cout was simply to test the functions and prove to me that it works.

Thanks again
Lordoftools

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 9
• 11
• 21
• 10
• 14
• Advertisement