Wednesday, February 23, 2011

How to get filename from given file path using c language or c++ language

#include <stdio.h>
#include <conio.h>
#include <string.h>
static int count=0;
static int pathsize;

void main()
{
char *path;//="C:\\windows\\explorer.exe";
char newpath[255];
int correctflag=0;
printf("Enter the file path : ");
scanf("%s",newpath);
path=newpath;
int i=0;
while(*(path+i)!='\0')
{
if(*(path+i)=='.')
correctflag=1;
i++;
}

int j=0,from=0;
i=0;
if(correctflag==1)
{
while(*(path+i)!='\0') //this will get the file path size....
{
pathsize++;
i++;
}
printf("\nGiven File path : %s\n",path);
printf("\nFile name : ");
for(i=pathsize;*(path+i)!='\\';i--)// this will count the number of characters before first backshash
{
count++;
}
from=pathsize-count;//from where file name starts this will get that point

for(i=from+1;*(path+i)!='.';i++)
{
printf("%c",*(path+i));

}
}
else
printf("\n Not correct path entered\n");
getch();

}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...