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.

>>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;

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.

>>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.

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.

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.

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 );

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.