Solved

string manipulation problem

Posted on 2002-07-27
1
194 Views
Last Modified: 2010-04-15
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
Comment
Question by:changchyi
1 Comment
 
LVL 1

Accepted Solution

by:
RolandAZ earned 50 total points
ID: 7182058
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now