question about opening files with fstream
May 26, 2010 at 1:20am UTC
im trying to open a file located at C:\Users\*USERNAME*\AppData\Roaming\FileZilla
The problem is everyones username is different so I used
1 2
char homedirectory[MAX_PATH];
SHGetFolderPathA(NULL,CSIDL_PROFILE,NULL,0,homedirectory);
to get the home directory.My problem is well I don't know how to explain it. It would probably look like this but this obviously isn't allowed
filestr.open(homedirectory"/AppData/Roaming/FileZilla/sitemanager.xml" ,fstream::in);
How could I do something like that?
May 26, 2010 at 2:16am UTC
Consider the .c_str() function, and using strings.
Once you get the string that is the home directory, something like this could work:
1 2 3
//psuedo/incomplete
string homedir = gethomedir();
fopen( homedir.c_str() + "rest/of/path" );
May 27, 2010 at 12:04am UTC
can you give me an example?
May 27, 2010 at 10:16am UTC
What is that Desh? Appending to char * with operator+??
Sorry saw your comment too late.
Do this (or similar)
1 2 3 4
//do what you did before and then
std::string hd(homedirectory);
hd += "/AppData/Roaming/FileZilla/sitemanager.xml" ;
filestr.open(hd,fstream::in);
May 27, 2010 at 7:39pm UTC
I get an error when doing that. Heres my complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <fstream>
#include <iostream>
#include <string>
#include <Shlobj.h>
using namespace std;
int main()
{
char homedirectory[MAX_PATH];
//Retrieves Users Home Directory
SHGetFolderPathA(NULL,CSIDL_PROFILE,NULL,0,homedirectory);
fstream filestr;
string filelocation(homedirectory);
//Starts Filezilla Password Recovery
filelocation+="/AppData/Roaming/FileZilla/sitemanager.xml" ;
filestr.open(filelocation,fstream::in);
filestr.close();
return 0;
}
and the error is
1>------ Build started: Project: Password Recoverer, Configuration: Release Win32 ------
1>Compiling...
1>Main.cpp
1>.\Main.cpp(15) : error C2664: 'void std::basic_fstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Users\Chris\Documents\Visual Studio 2008\Projects\Password Recoverer\Password Recoverer\Release\BuildLog.htm"
1>Password Recoverer - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
May 28, 2010 at 2:37am UTC
You need to convert the std::string to a C string. Like this:
filestr.open(filelocation.c_str(),fstream::in);
May 28, 2010 at 8:17pm UTC
thanks that worked
May 29, 2010 at 1:37pm UTC
What is that Desh? Appending to char * with operator+??
Yea, quickly did that to get the point accross. I meant to create the string, append the "rest_of_dir" to it, THEN convert it to a c style string using c_str().
Topic archived. No new replies allowed.