Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

string manipulation problem

I face difficulty when i try to extract the path from a given full file path name:

C:\abc\xyz\data.txt

In what way i can combine the string manipulation function in C in order to extract out the path:

C:\abc\xyz\

??
0
changchyi
Asked:
changchyi
1 Solution
 
RolandAZCommented:
hi changchyi

basically, its

  _splitpath(fullname,drive, dir, fname, fextension);
  strcpy(thepath,drive);
  strcat(thepath,dir);
   
/////

Here is a test program.
This works well on a win2k , should be portable to all platforms:

//--------------------------------------
//includes for the _splitpath and string routines
#include <stdlib.h>
#include <string.h>
//or #include <wchar.h> for wide chars

//includes for printf and getch
#include <conio.h>
#include <stdio.h>

//test program
int main(int argc, char* argv[])
{

     //data variables
      //change "char" type to wchar_t if  using wide chars
char drive[_MAX_DRIVE];   //change buffer sizes to
                         // suit your requirements
char dir[_MAX_DIR];    
char fname[_MAX_FNAME];
char fextension[_MAX_EXT];
char thepath[_MAX_PATH];

//your full name
char fullname[_MAX_PATH] =  "C:\\abc\\xyz\\data.txt";


//code

//start of the path string manipulations

  _splitpath(fullname,drive, dir, fname, fextension);
  strcpy(thepath,drive);
  strcat(thepath,dir);
   
//end string manipulations

//test print to see if we have the path
  printf(thepath);
  while(getch()!='X');
  return 0;
}

//------------------------------


Hope that is what you needed, Changchyi!

have fun!
Roland






0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now