1

I currently have a char array that is storing a filename.

eg. folder1/subfolder/subsubfolder/file1.txt

I want to be able to remove the first folder name in this char array. so it becomes

subfolder/subsubfolder/file1.txt

I know that basename() will make the output file1.txt but I need the folder structure, just not the first one.

What is the best way to do it? use / as a delineating character?

  • Edit * Ok apparently I can't ask about a char array without going in to detail about my whole freaking program lol.

I would like to know is if there is a simple way to shorten a char array from the beginning. I am working on a file copying program and it would just make my life a lot easier if I can shorten the full file location by discarding the first folder name. I am a complete noob when it comes to programming and thought this might have been an easy one to solve for the wizards on this site. I didn't put code in my initial post because I don't know if the is even a function that does what I'm asking. So apologies. But if you have to have code then here...

        strcpy(sourcePath, Path); 
    strcat(sourcePath, slash);

This obviously doesn't do what I need it to do.

user2661167
  • 491
  • 5
  • 13
  • 22
  • 1
    Find the first delimiter, and use `strcpy` to pick out what follows – David Heffernan Sep 15 '13 at 14:36
  • 1
    Off-topic -> Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance. – dhein Sep 15 '13 at 14:37
  • Thats the problem, how do get the program to automatically do this? I've used strcpy but only from one char array to another, never editing in between. – user2661167 Sep 15 '13 at 14:39
  • @Zaibis I don't think its off-topic. Its a simple and directly question, as the duplicate of it has positive votes. It just don't show research effort. – Diego C Nascimento Sep 15 '13 at 14:42
  • @diego c nascimento "It just don't show research effort" -> thats reason enough to be off-topic to SO. – dhein Sep 15 '13 at 14:45
  • 2
    Zaibis, I searched the forum and google for examples and mostly see examples of reducing an array based on numbers, not a delineating character. Either that or previous threads that deal with my problem were not Titled correctly to draw my attention. So for you to give me a hard time really is a shame cos I thought this website was about helping people not griefing, but I guess some people have superiority complexes. – user2661167 Sep 15 '13 at 14:49

3 Answers3

2

You can use strpbrk

const char * strpbrk ( const char * str1, const char * str2 );

http://en.cppreference.com/w/c/string/byte/strpbrk

It returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

In your case str2= "/"

// You can use it in the following way
const char* f = strpbrk(pathname, str2);
int sz = strlen(pathname) - strlen(f); // +/- 1.
new_path_name = strncpy(new_path_name, f, sz);
A. K.
  • 34,395
  • 15
  • 52
  • 89
1

you can use this to help you substring:

char subbuff[5]
memcpy( subbuff, &buff[10], 4 );
subbuff[4] = '\0';

and this to help you find index of:

size_t strcspn ( const char * str1, const char * str2 );

and an example:

/* strcspn example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "fcba73";
  char keys[] = "1234567890";
  int i;
  i = strcspn (str,keys);
  printf ("The first number in str is at position %d.\n",i+1);
  return 0;
}
Community
  • 1
  • 1
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

You can use this part to remove the specific word from an array

for (i = 0; i < slength; i++)
{
    if (str[i] == word[j])
        j++;
    else
        j = 0;

    if (j == wlength)
        for (t = 0; t <= wlength; t++)
        {
            for (k = 0; k < slength; k++)
                str[i - j + k] = str[i - j + k + 1];
        }
}