-2

Hi following is the path of a file (which is stored as a string).

C:/db/OOSA/LIBRARIES/OOSA00/MS/filename.jpg

now I want only the file name from that for eg: "filename", rest should be filtered or removed. How to do that in C? I want to apply that file name to some other stuffs but i want to avoid .jpg extension and the path " C:/db/OOSA/LIBRARIES/OOSA00/MS/" Below is the code:

   static mgbool gbeadNApply (mgrec* db, mgrec* parent, mgrec* rec, void* gBead)
{
   toolrec* toolRec = (toolrec*)gBead;

   if (mgGetCode(rec) == fltXref);
   {
           char *xName;
           parent = mgGetParent(rec);
           mgGetAttList(rec,fltXrefFilename,&xName,MG_NULL);
           mgSetName(parent,xName);
       }

   return MG_TRUE;
}

Here xName first collects the filename including path. and in mgSetName also you can see xName ( here xName assigns the collected file name along with path some thing like C:/db/OOSA/LIBRARIES/OOSA00/MS/filename.jpg. Now the thing is I want only the filename part of it to be written to mgSetName. so i want to filter xName for it.

1 Answers1

0

This will be very vague, but you can probably figure out how to write a function to do this:

  1. Write a function to find the "."
  2. Write a function that returns, in a new string, everything before the "."
  3. Write a function that finds the last "/" ( backslash? "\")
  4. Write a function that removes everything before and including the "/"

you will use for loops

int find_period(const char *string)
{
  if(!string) return -1;

  int n;
  for(n = 0; n < strlen(string); n++){
    if(string[n] == '.') return n;

    return -1;
}

Then you probably get the general idea.

Gophyr
  • 406
  • 2
  • 11
  • 2
    1. reinvent the wheel 2. reinvent the wheel 3. reinvent the wheel – Karoly Horvath Dec 09 '14 at 18:23
  • I'd rather locate the last backslash first, as a precaution against extensionless files and directory names containing full stops. – Jongware Dec 09 '14 at 18:24
  • I am a beginer in C. so dont know where to tweak or add. please someone hep me to achieve this, – Sarath Sankar Dec 09 '14 at 18:36
  • 1
    @SarathSankar didn't write the code posted, considering the level of the question asked, and the acceptance of this answer. I suggest you find and follow a string handling tutorial, to accustom you to the available library functions. – Weather Vane Dec 09 '14 at 18:41
  • i concur with that statement. – Gophyr Dec 10 '14 at 12:29