|
Sorry, but you will need to be much more explicit because I'm lost.
mac_g wrote: but i want to try to specify the path in code itself.
That was what I explained you: just pass a string which contains the path.
mac_g wrote: in code there is a msg
"pls input the directory or path where you save the file"
In code ? You mean you display that message on the console and wait until the user type the path ? If yes, just retrieve the string he typed and pass it to the ifstream constructor, that's as easy as that.
So, where are you stuck exactly ? In the part where you have to retrieve the string from the user or passing the string to the constructor ?
Please also explain what you already did and what is the exact problem you encounter.
|
|
|
|
|
can you give me an example for retrieving the string to the ifstream constructor?
thanks
|
|
|
|
|
Do you mean like this:
ifstream iFile("c:\\SomeDirectory\\AnotherDirectory\\input.txt");
or maybe you want the path in a separate variable and you want to add the filename onto the path?
#include <shlwapi.h>
void UseFile(const char* directory)
{
char path[MAX_PATH];
PathCombine(path, directory, "input.txt");
ifstream iFile(path);
The Boost FileSystem[^] library is another way of doing that - that will become part of the C++ standard libraries sometime in the future.
|
|
|
|
|
yes..thank you...
|
|
|
|
|
Hi all,
Is it possible to convert an String which is identical to an enum symbol to an enum value?
Currently I am converting an string _T("ONE") into an enum symbol ONE using string comparison as indicated by the code snippet below.
in a .h file
enum
{
ZERO = 0,
ONE,
TWO,
THREE
};
{
int myenum = 0;
TCHAR szNumber[MAX_PATH]={0};
_tcscpy(szNumber, _T("ONE"));
ConvertStrtoEnum(szEnum, myenum)
}
static void ConvertStrtoEnum(TCHAR &szEnum, int myenum)
{
if(!_tcscmp(szEnum, _T("ZERO"))
myenum = ZERO;
else if(!_tcscmp(szEnum, _T("ONE"))
myenum = ONE;
.
.
.
return;
}
is there another alternative to achieving the same result?
Any guidance will be helpful.
Regards.
|
|
|
|
|
No, you can't do that (not by a standard way anyway).
For what purpose do you need to have that mechanism ? In general , when you need to have something like that, it means something is wrong with your design (but it's not true in all cases)...
|
|
|
|
|
The application that I am developing reads data (one of the data is of type enum)from the xml.
Rather than having numbers in the xml file, I wanted to include the enum symbols itself(strings) which is readable and self explainatery in the xml file.
It is left to my application to read the strings and convert it to the corresponding enum value.
Is there any alternative approach to this? (the other apporach being mention the enum value in the xml itself)
|
|
|
|
|
Comp_Users wrote: Rather than having numbers in the xml file, I wanted to include the enum symbols itself(strings) which is readable and self explainatery in the xml file.
Honnestly, in this specific case, I don't really agree with you: writing ZERO instead of 0 in the xml file doesn't make it clearer. If it is a number, why do you want to write it as a string ? Well, it's just my opinion...
|
|
|
|
|
<blockquote class="FQ"><div class="FQA">Comp_User wrote:</div>is there another alternative to achieving the same result?</blockquote>
Not without some bit of programming.
I often use a std::map (I've used the Boost Assign[^] library as well, as it makes container so much easier and Boost Optional[^] to provide for the case when no enumerals match the string) like this:
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>
#include <boost/optional.hpp>
enum XX { AA, BB, CC, DD, EE };
boost::optional<XX> XXFromString(std::string const& s)
{
#define ITEM(I) (#I, I)
const std::map<std::string, XX> MapStringToXX = boost::assign::map_list_of ITEM(AA)ITEM(BB)ITEM(CC)ITEM(DD)ITEM(EE);
#undef ITEM
std::map<std::string, XX>::const_iterator itEnum = MapStringToXX.find(s);
return (itEnum == MapStringToXX.end())?boost::optional<XX>():itEnum->second;
}
I've used the ITEM macro so I only have to type enumerals once - the macro expands to a string of the enum and a token of the enumeral.
|
|
|
|
|
Stuart,
I am not allowed to use any additional library like the Boost Assign library that you have made use of.
could you please give me a simpler example to demonstrate as to how you achieve the conversion?
Thanks.
|
|
|
|
|
You may do something similar to the following:
enum.txt file
ENUM(ONE)
ENUM(TWO)
ENUM(THREE)
ENUM(FOUR)
source code
#define ENUM(s) s,
enum Numbers
{
#include "enum.h"
NUMBERS
};
#undef ENUM
int stringtonumber(char * str)
{
#define ENUM(s) #s,
static char * tok[NUMBERS+1] =
{
#include "enum.txt"
NULL
};
#undef ENUM
int i=0;
while ( tok[i] )
{
if ( ! strcmp(tok[i],str ) )
return (i+1);
i++;
}
return -1;
}
void main()
{
printf("%s is %d\n", "THREE", stringtonumber("THREE"));
}
The sample code is silly, but you possibly got the idea.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
OK - just using std::map (I presume you're allowed to use the C++ standard libraries?):
enum XX { AA=1, BB, CC, DD, EE };
std::pair<bool, XX> FromString(std::string const& s)
{
static std::map<std::string, XX> MapStringToXX;
if (MapStringToXX.empty())
{
#define INSERT_ITEM(I) MapStringToXX.insert(std::make_pair(std::string(#I), I))
INSERT_ITEM(AA);
INSERT_ITEM(BB);
INSERT_ITEM(CC);
INSERT_ITEM(DD);
INSERT_ITEM(EE);
}
#undef ITEM
std::map<std::string, XX>::const_iterator itEnum = MapStringToXX.find(s);
return (itEnum == MapStringToXX.end())
?std::make_pair(false, XX())
:std::make_pair(true, itEnum->second);
}
This time, the map is a static variable that you initialise the frist time the function is called, while we return a (bool, XX) pair, using the bool item to show if the string was found or not.
Warning: this code isn't going to be thread-safe...
|
|
|
|
|
Comp_User wrote: static void ConvertStrtoEnum(TCHAR &szEnum, int myenum)
{
if(!_tcscmp(szEnum, _T("ZERO"))
myenum = ZERO;
else if(!_tcscmp(szEnum, _T("ONE"))
myenum = ONE;
.
.
.//Do the same for all enum values.
Instead of this, I suggest you to store the strings in an array and iterate... then the index can be used as corresponding enumID.
For eg.,
int GetIdx( LPCTSTR lpctszID )
{
const MAX_IDS = 3;
LPCTSTR strRef[MAX_IDS]={ _T( "ONE" ), _T( "TWO" ), _T( "THREE" ) };
for(int nIdx = 0; nIdx < MAX_IDS; nIdx++)
{
if( 0 == lstrcmp( lpctszID, strRef[nIdx] ))
{
return nIdx;
}
}
return -1;
}
But if the enum is not sequential then there needs another similar method to iterate the enum ID.
- ns ami -
|
|
|
|
|
Dear all,
I have a C++ unmanaged COM object which is being used in a C## application.
Sometimes, when I raise event from COM object I get an HRESULT with below message:
The process cannot access the file because it is being used by another process.
I can not understand how this message relates to event raising.
Please help me about this weird situation If you have similar experience.
Regards
|
|
|
|
|
Hi all
i am geting error
Unhandled exception at 0x00529f01 in DemoUSB.exe: 0xC0000005: Access violation reading location 0x00000000. . and application creash at this point
ATL_NOINLINE void Fork( __in int nLength )
{
CStringData* pOldData = GetData();
int nOldLength = pOldData->nDataLength;
CStringData* pNewData = pOldData->pStringMgr->Clone()->Allocate( nLength, sizeof( XCHAR ) );
if( pNewData == NULL )
{
ThrowMemoryException();
}
When i debug the code then i am not geting any exception but application run regular then exception generated.
Plz help me
|
|
|
|
|
The message "Access violation reading location 0x00000000." means that you are accessing a pointer variable without assigning a memory.
Could you please check the pointers in CStringData* pOldData = GetData(); and CStringData* pNewData = pOldData->pStringMgr->Clone()->Allocate( nLength, sizeof( XCHAR ) ); lines.
( My suggestion is to check all pointers in CStringData* pNewData = pOldData->pStringMgr->Clone()->Allocate( nLength, sizeof( XCHAR ) ); )
In the debug mode, debugger is allocating and controlling memory access. If you use an unallocated pointer, the output will be undefined. Sometimes it will show exception/assertion or sometimes works fine. So it's better to check the pointers before use.
aks
|
|
|
|
|
Hi friends,
We have two applications . one is OLE container and another is OLE server application. It was built with 2005 and old MFC classes such as CWinApp, CFrameWnd and CToolbar classes. It was working fine. We were able to embed the server application in container application.
We have migrated the application to 2008 . Now we are using new MFC feature pack classes CWinAppEx,CFrameWndEx and CMFCToolbar. Individually, both application are working fine. But we are not able to embed the server application in container application.
Can anyone provide the solution on this ? any sample application avialable for OLE server - container with VS2008 MFC feature pack classes.
|
|
|
|
|
Hi All,
I got a link error while compiling my project.The error is in another module ie in another .obj .This is static link library.Can anyone help me to find a solution .The error is given below.
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl std::basic_string<char,struct>,class std::allocator<char> >::basic_string<char,struct>,class std::allocator<char> >(class std::basic_string<char,struct>,class std::allocator<char> > const &)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@AEBV01@@Z) referenced in function "public: __cdecl std::logic_error::logic_error(class std::basic_string<char,struct>,class std::allocator<char> > const &)"
Thanks & Regards
VIJITH VIJAYAN
|
|
|
|
|
1. Are you sure that your application linked with proper lib
2. Are you using any third party libraries like STL Port or something? then make sure you've put it in the right include order
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
yes i had linked .And thanks for the quick reply
Thanks & Regards
VIJITH VIJAYAN
|
|
|
|
|
Hi all,
I m using Killfoucus function for button control but its not working.
i add this from Control events of Button control BN_KILLFOCUS.
please tell me what can i do for this.
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
Is your button having BS_NOTIFY style? You can set this style in the resource editor, if you are using it (Properties > Styles > Notify).
- ns ami -
|
|
|
|
|
Thanks its done.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
You are welcome...
- ns ami -
|
|
|
|
|
Create a button class, say CButtonEx, derived from CButton and map the ON_WM_KILLFOCUS message.
Change the class of the button control that you have to CButtonEx.
aks
|
|
|
|