0

Hi all,

I have a string(not a CString), actually a file path. As an example,

c:\Test\MyProject\G00062_002_01.srf

Then in following way get some data.

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.

3
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Duoas
0

>>string filePath = FullPath.GetFileTitle();
std::string does not have a method called GetFileTiele(). That is in the CStirng class :)

you can use stringstream class

#inlcude <sstring>
...
int ID;
stringstream stream(groupID);
stream >> ID;
0

Thanks, your way is working.

One thing I'm confusing. I'm using a string type variable, not a CString. Actually in an MFC application.

0

>>One thing I'm confusing. I'm using a string type variable, not a CString. Actually in an MFC application.

What's so confusing about that? Just because you have an MFC project doesn't mean you can't use the c++ STL and other classes such as io streams. I like MFC and CString for interacting with MFC object. But I find std::string can be more useful for other things. And I normally never use CFile class because it sucks cannel water. fstream is much better, unless you want to serialize MFC objects.

0

Yep, you are right. I agreed with you that I can use strings in MFC, no question about that. And also I feel that learning pure/standard C++ is more better.

What I'm confusing is that, as far as I found from several articles, as a simple standard CStrings use in MFC projects, not strings. I don't know how far I'm clever on this sentences actually.

0

CStrings are required when interacting with MFC objects such as CEdit, CListBox, etc. Other than that, you can use whatever you like. Using std::string might bloat your program up a little though. If you are concerned about the size of the final executable program then try to avoid std::string.

0

It is easy enough to write something that works with std::string.

std::string GetFileTitle( std::string filename ) {
  std::string result;
  short len = GetFileTitle( filename.c_str(), NULL, 0 );
  if (len < 0) throw 1;
  result.resize( len -1, '\0' );
  if (!GetFileTitle(
    filename.c_str(),
    const_cast<char *>( result.c_str() ),
    len
    ))
    throw 1;
  return result;
  }

There are several ways to deal with errors. In this example I just throw an int exception. You may want to adjust it to throw something else or return an empty string or whatever strikes your fancy.

Enjoy.

[EDIT] Oh yeah, an example of use: string FullPath = "c:\fooey\quux.txt"; string filename = GetFileTitle( FullPath );

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.