Solved

string manipulation problem

Posted on 2002-07-27
1
212 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pointer in one class to member in another 6 122
First character of input string truncated with scanf 3 99
Acrinis True image 2 79
Test the speeds on my PC Drives 12 48
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

831 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