Solved

string manipulation problem

Posted on 2002-07-27
1
223 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
[X]
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
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

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!

Question has a verified solution.

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

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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

733 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