Results 1 to 8 of 8

Thread: int value through string

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink 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

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: int value through string

    Quote 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

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  8. #8

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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
  •  



Click Here to Expand Forum to Full Width