×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

C++ String Manipulation Question

C++ String Manipulation Question

C++ String Manipulation Question

(OP)
I am a new C++ programmer that has been thrust into the belly of the C++ beast.  I am having problems with trying to extract a substring from a string.  The string and substrings will be of varying length, what I do know is I want to start grabbing the text after the last '\' until and keep grabbing until i hit the last '.'.  Yes, I am trying to pull a piece of a file string.

ie...

str = "\\ddata\conf\src\bin\d_5032.dat"

I am trying to pull the "d_5032" from the string above and I want to assign it to a variable.  I am using MS Visual C++ 6.0.  Any help would be greatly appreciated.

RE: C++ String Manipulation Question

I suppose that you take the string from a file content.

Hope this help you...

--
char *aux1,aux2[7],buffer[50],var[200];
// read a file line content up to the end
// of the line in the present string

  fgets(buffer,200,file2);
  sscanf(buffer,"%s", &var);
  aux1=&var[0];
  count=0;


// it gets the length of the captured line/string
// and put the
// aux1 vector at the end of the line/string_vector
aux1=&buffer[0];
aux1+=strlen(buffer);
for(i=0;if(!strcmp(buffer,"\"));i++)
{ // you move back the position to the
  // begining of the string and stop it
  // when you find the character "\"
  aux1--;
}

for(i=0;i<fileN_character;i++)
{ // now you copy letter by letter from the file name
  aux2=*aux1; aux1++;
}

// yet at aux2 you have the file name!...{:ß).

--
Tom

RE: C++ String Manipulation Question

Were you wanting to use std::string member functions? If so, there's one called find_last_of() that returns an index into the string of the last character or string that you send it:

string str("\\ddata\\conf\\src\\bin\\d_5032.dat"),title;
string::size_type idxslash,idxdot;
    
idxslash=str.find_last_of('\\');
if (idxslash==str.npos)
  idxslash=0;// couldn't find a '\', start at 0    
else
  ++idxslash; // go to next character after last '\'

idxdot=str.find_last_of('.');
if (idxdot==str.npos)
  idxdot=str.size();    // couldn't find a '.', go to end

// Extract title (between last '/' and last '.')
title=str.substr(idxslash,idxdot-idxslash);

RE: C++ String Manipulation Question

You could also use the windows API function: void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext) in <stdlib.h>. For (my) convenience I use this in a C++ wrapper and return std::string values. One also needs to use the char *_getcwd(char *buffer, int maxlen) if you are interested in the path name of a file in the current directory.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources