|
To convert from CString to std::string or from std::string to CString use the following:
CString -> std::string
<br />
CString csTest = "test";<br />
std::string strTest = static_cast<LPCTSTR>(csTest);<br />
std::string -> CString
<br />
std::string strTest = "Test";<br />
CString csTest = strTest.c_str();<br />
As simple as that.
have a nice day.
Alexandre
|
|
|
|
|
Hmm, don't know which platform you're using, but when I do this (compiling for the PocketPC platform) I get the following error:
error C2440: 'initializing' : cannot convert from 'LPCTSTR' to 'std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
Still haven't found a solution that works for me though...
|
|
|
|
|
PocketPC projects are Unicode by default, so CString is actually CStringW, and must be converted to std::wstring.
|
|
|
|
|
After converting from CString to std::string it is giving following error,as source code is in UNICODE.
error C2440: 'static_cast' : cannot convert from 'class CString' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
gn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
hn
hn
ghn
ghn
ghn
ghn
ghn
ghn
ghn
gn
hn
hgn
gh
nh
|
|
|
|
|
Hi
I cannot compile your code through VC 6 Prof. it says
--------------------Konfiguration: yabt - Win32 Debug--------------------
Kompilierung läuft...
IniFile.cpp
D:\Projekte\yabt\extern_code\IniFile.cpp(155) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_s
tring<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : Bezeichner wurde auf '255' Zeichen in den Debug-Informationen reduziert
D:\Projekte\yabt\extern_code\IniFile.cpp(155) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<
char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : Bezeichner wurde auf '255' Zeichen in den Debug-Informationen reduziert
D:\Projekte\yabt\extern_code\IniFile.cpp(241) : error C2552: 's' : Initialisierung nicht zusammengesetzter Typen mit Initialisierungsliste ist nicht moeglich
D:\Projekte\yabt\extern_code\IniFile.cpp(242) : error C2552: 'r' : Initialisierung nicht zusammengesetzter Typen mit Initialisierungsliste ist nicht moeglich
D:\Projekte\yabt\extern_code\IniFile.cpp(254) : error C2552: 'r' : Initialisierung nicht zusammengesetzter Typen mit Initialisierungsliste ist nicht moeglich
D:\Projekte\yabt\extern_code\IniFile.cpp(504) : error C2552: 's' : Initialisierung nicht zusammengesetzter Typen mit Initialisierungsliste ist nicht moeglich
Generieren von Code...
Fehler beim Ausführen von cl.exe.
XnVersion.exe - 4 Fehler, 2 Warnung(en)
Any workaround?
Thy anyway
-------------------
http://www.sanji.tk
|
|
|
|
|
Yeah I get the exact same problem...although I would add that the .exe that you included in the download works fine.
Looks like a nice utility but I'm obviously doing something wrong.
Cheers
Andy
|
|
|
|
|
the faulty code is the struct initialization at lines
241, 242, 254 and 504
change the "{"",' ',SectionName,"",""}" for
s.Comments = "";
s.Commented = ' ';
s.Section = SectionName;
s.Key = "";
s.Value = "";
where s is the Record object;
Somehow VC++ 6.0 does not like the initialization used. I guess
VC++ .NET/2003 does not complain about this.
have a nice day,
Alexandre
|
|
|
|
|
What he means is:
Find lines: 241, 242, 254 and 504
change the "{"",' ',SectionName,"",""}" To:
Record s;
s.Comments = "";
s.Commented = '\0'; //This prevents empty character constant error.
s.Section = SectionName;
s.Key = "";
s.Value = "";
Do the same for the other lines and it should work.
Douglas Saltsman
www.primopdf.com <-- Download my free PDF creator
|
|
|
|
|
I had the same compilation erros, but I have fixed them in another way. Instead of assigning each single attribute, I have created constructors for the struct CIniFile::Record (remember that in C++ "struct" constructions are just like "class" constructions, except by the fact that the former has "public" as default visibility).
The code changes in CIniFile::Record are shown below:
struct Record
{
string Comments;
char Commented;
string Section;
string Key;
string Value;
Record() { Commented = ' '; }
Record(const string& commentsStr, char commented, const string& sectionStr,
const string& keyStr, const string& valueStr)
: Comments(commentsStr), Commented(commented), Section(sectionStr), Key(keyStr), Value(valueStr)
{ }
};
Now, all you have to do is change faulty lines like that:
Record r = {"",' ',SectionName,KeyName,Value};
to the proper use of the new constructor:
Record r ("",' ',SectionName,KeyName,Value);
I hope someone will find it useful...
|
|
|
|
|
after this fix:
--------------------Configuration: Object - Win32 Final--------------------<br />
Compiling...<br />
IniFile.cpp<br />
C:\proj\AVP2\ObjectDLL\IniFile.cpp(132) : warning C4018: '!=' : signed/unsigned mismatch<br />
\lithshared\stl\stl/_algo.c(88) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingSectionSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::DescendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(88) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(89) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(91) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(95) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2664: '()' : cannot convert parameter 1 from 'const struct CIniFile::Record' to 'struct CIniFile::Record &'<br />
Conversion loses qualifiers<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
\lithshared\stl\stl/_algo.c(97) : error C2064: term does not evaluate to a function<br />
\lithshared\stl\stl/_algo.c(1152) : see reference to function template instantiation 'const struct CIniFile::Record &__cdecl _STL::__median(const struct CIniFile::Record &,const struct CIniFile::Record &,const struct CIniFile::Record &,struc<br />
t CIniFile::AscendingRecordSort)' being compiled<br />
Error executing cl.exe.<br />
<br />
Object.lto - 40 error(s), 1 warning(s)<br />
The following environment variables were not found<br />
$(LT2_DIR)<br />
$(AVP2_BUILD_DIR)
|
|
|
|
|
Hello!
I have some comments to your code, I think it is very expensive to use:
Why are all functions static?
Most of the time I want to load/save the INI File once or only very seldom, at startup or when the user changes the preferences. So, simply make it a normal class, make filename and the content vector a member and thats it.
When I want for some reasons to reload/save the data, I can do it with the Load/Save memberfunctions. But there is no need to enforce that it is done automatically.
Some lesser improvements:
1) Please do not add the line:
using namespace std;
to your header file!
With it you defeat the whole idea behind putting something in a namespace. Everywhere youre header is included, the whole namespace gets included.
2)
Also, in the header you do not need to include e.g. iostream because you only need it in the cpp file. While this is more of an optimization please try to minimize the headers you include in headers.
Everybody who uses a compiler with no precompiled header feature will be grateful.
3) Use const&:
e.g.
static bool AddSection(string SectionName, string FileName);
should get its parameters by const& because there is no need that the strings are copied
4) Errorchecking on streams.
You do no errorchecking if a read access to the inifile fails. While an eror is unlikely you still should protect against it.
Maybe use:
while(!std::getline(inFile, s)))
{ ... }
if (inFile.eof()) return true;
else return false;
This way you could also remove the test for is_open.
5) Do not use endl. For better performance use '\n'. Use endl only if you really intend to flush the stream. You only want this seldom with files.
cu
|
|
|
|
|
|
Hmmm... hadn't thought about it, but sure - that could be helpful, especially if all you use are integer values. It would at least save you the trouble of converting to/from a string.
Sure, I'll add it to the next "version", whatever that is
Thanks for the suggestion.
-Todd Davis (toddhd@gmail.com)
|
|
|
|
|
|
|
This is because the precompiled header and object files are in the zip file. This will probably get cleaned up by the Code Project team.
Apart from that the code is very clean.
Yves Tkaczyk
|
|
|
|
|
Please fix the file. Many people cannot (or will not, or do not want to) download .pch .
|
|
|
|
|
Sorry about that guys - I just zipped up the whole VS project - I'm spoiled by highspeed internet
I rezipped it up with just the source files, and the (Windows only) executable. Now its only 68k. Does that help?
-Todd Davis (toddhd@hotmail.com)
|
|
|
|
|
Thanks! Great and useful class, will keep my eye on its development!
|
|
|
|
|
hi todd,
this may be a pointless question because of the cross-platform nature of the code but would there be any value to being able to import from/export to the windows registry.
i've writen a freeware app that allows users to store settings either in the registry or a .ini file but at present does not provide the ability to switch later on.
i'm not asking you to do the work for me but am just interested in your response.
rgds
.dan.g.
AbstractSpoon Software
|
|
|
|
|
I think there is a ton of value in it actually - what I'm not sure is if I would try to handle that in this class. A friend of mine on GameDev.net pointed out the way he handles it - he creates a Config class that handles configuration settings in a generic way - then subclasses are called which pull the data from the ini file/xml/registry/whatever so that future changes can be made invisibly. Here is some pseudo-code to explain further:
class IConfig
{
public:
...
virtual bool ReadSetting(const TCHAR * Setting, std::string & Value) = 0;
virtual bool ReadInt(const TCHAR * Setting, int & Value)
{
std::string StrValue;
if(ReadSetting(Setting, StrValue))
...
return false;
}
virtual void WriteSetting(const TCHAR * Setting, const TCHAR * Value) = 0;
...
};
class CIniConfig : public IConfig
{
// use .ini files
};
class CXmlConfig : public IConfig
{
// use .xml files
};
class CRegistryConfig : public IConfig
{
// use windows registry
};
-Todd Davis (toddhd@hotmail.com)
|
|
|
|
|
You mean your VB program can be run on UNIX, LINUS, Apple OS..... and Window(s)?
Woww, you are so great. Perhaps you are the FIRST person who can run vb app on Unix/Linus/Apple OS.....
YOU ARE THE MAN!!!!
by the way, I do not see any reason should use .ini in .Net environment.
Perhaps, config file is much better than .ini
Log4net shows me a great example of using config file.....
|
|
|
|
|
Thanks for those really productive comments A@damn. Let me clear a few things up for you.
First, this is not a VB progam.
Second, this is also not a .NET program.
I thought that the title of this article, "CIniFile Class for C++ - A robust cross platform Ini file class" would have made those points clear, perhaps I'll need to be more careful in the future.
By the way, my VB.NET programs *can* run on Unix, Linux and Apple OS (which most people refer to as the Mac OS, but you are much smarter than I). And no, I am not the first person to accomplish this. It was accomplished by lots of hard working, open source folks, and you can learn more about it by visiting the Mono project:
http://www.mono-project.com/about/index.html
I do agree with you about two things however - I am great, and I am the man.
Finally, my *other* article is a VB.NET Ini file - as other this writing, it has 15,677 views, an excellent rating, and I have had so many people email me about it that I had to get an alternate email address to forward the emails to. It is unfortunate that you can't see why an ini file would be useful in a .NET environment, but fortunately, not everyone is as short-sighted as you. Bottom line, you use what works, is easily deployable and supportable, and leaves the door open for cross platform and cross application data management. The venerable ini file can't be beat, even by XML, in straight forward readability.
Thanks for making my day
-Todd Davis (toddhd@hotmail.com)
|
|
|
|
|
I forgot that *we" have mono....
Did I say: you are the MAN!!? Did I?
|
|
|
|
|
Todd
You asked in your artcle for suggestions as to how to do all the key comparisons in one case (upper or lower) so that they are not case sensitive yet preserve the case when saving the file.
I'm a bit of an amateur (so please forgive what may be a very lame idea) but my approach would be to have a struct (or class) in which the keys are duplicated, one containing the original mixed case strings (used for writing back to the file) and the other converted to upper case for the comparisons. It makes the class a little bigger and adds a little overhead but it might be worth it.
Sincerely
Simon Rodan
|
|
|
|
|