Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

string manipulation problem

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 how to create, access, and change arrays in the C programming language.

839 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