|
-
Dec 5th, 2007, 12:16 AM
#1
Thread Starter
PowerPoster
int value through string
Hi all,
I have a string(not a CString), actually a file path. As an example,
Code:
c:\Test\MyProject\G00062_002_01.srf
Then in following way get some data.
Code:
string FullPath = c:\Test\MyProject\G00062_002_01.srf
string filePath = FullPath.GetFileTitle();
string groupID;
string sessionId;
string partID;
groupID = filePath.substr(1, 5);
sessionId = filePath.substr(7, 3);
partID = filePath.substr(11, 2);
So my outputs are 00062, 002, 01 respectively as string type. I want to add those values in a database as int type. How should I do this conversion.
Where I'm confusing is that, outputs are in string format. But actually there are some integers.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 5th, 2007, 12:29 AM
#2
Re: int value through string
There are many ways to convert strings to integers.. one of which involves the atoi() function..
Other ways include using the STL to convert things for you.. using istringstreams and such..
Converting strings to integers, and integers to strings, are one of the most common C/C++ questions around. It shouldn't be hard to find examples.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 5th, 2007, 01:19 AM
#3
Thread Starter
PowerPoster
Re: int value through string
Ok,
I have done it in this way. What you think of it.
Code:
int grID;
stringstream stGroup(filePath.substr(1, 5));
stGroup >> grID;
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 5th, 2007, 06:36 AM
#4
Re: int value through string
 Originally Posted by eranga262154
Ok,
I have done it in this way. What you think of it.
Code:
int grID;
stringstream stGroup(filePath.substr(1, 5));
stGroup >> grID;
Thats pretty much what I mentioned above..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 5th, 2007, 06:46 AM
#5
Thread Starter
PowerPoster
Re: int value through string
Ok, one thing to clear.
As you said atoi() function is a C function, if I'm correctly refer the MSDN. If so is it better to mix-up C and C++ in an application?
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 5th, 2007, 07:05 AM
#6
Re: int value through string
You can't help but mix the two up.. they're almost completely the same 
Some people say using C helper functions is badbadbad if you're using a C++ compiler.. but who cares I say.. 90% of your other code is compilable with a C compiler ..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 5th, 2007, 07:47 AM
#7
Re: int value through string
Anything that is valid C is by definition valid C++ as well.
It's all a matter of taste really. A lot of people complain if you use printf() or malloc() in C++ programs, others don't. I use C++ a lot but I never use the string class, I prefer to do all my cstring (char*) manipulation manually. One guy I know has used C++ for 3 years and he didn't know how cstrings worked.
So it all depends on what you're used to.
Someone complaining about using C functions in C++ probably doesn't understand how the languages relate to each other. Such a person would scarcely be able to use a win32API call for example, because the vast majority of them are sympathetic to C data types.
Off the top of my head, I can't think of any win32 calls that demand a stream object or a referece to a form class...
You could argue that from a best-practice point of view you should use C++-only things like std::cout and so on which are syntactically unavailable in C. This is fair, but not the whole story. But that is beyond the scope of this thread sadly.
Just my $0.02 worth.
I don't live here any more.
-
Dec 5th, 2007, 10:16 PM
#8
Thread Starter
PowerPoster
Re: int value through string
Thanks a lot for all the replays.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|