get file extension

This is a discussion on get file extension within the C Programming forums, part of the General Programming Boards category; Hello in my program i go to specific directry .From this directry i read files names and write them in ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    get file extension

    Hello
    in my program i go to specific directry .From this directry i read files names and write them in the console BUT my problem is the following i want to display the names of files has (s) extension like the follwoing :
    a.s
    b.s
    a.txt
    now my approach to display files names which has extension s
    a
    b
    so i did already program display all files in directry BUT

    1 . how i can chech for file extension
    2.how i can display the file name without its extension

    Thanks a lot

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could use the library function strrchr() to get a pointer to the '.'. The characters after that will be the extension and the characters before will be the filename.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() 
    { 
    
    	char* ext;
    	char* p;
    	char fullname []="file.txt";
    	ext = strchr(fullname,'.');
    	printf("extension - %s\nfilename - ",ext+1);
    	for(p=fullname;p!=ext;p++)
    	{
    		printf("%c",*p);
    	}
    	printf("\n");
    	
    }
    zen

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    34

    Post

    /******
    The following code should work for all combinations of file names you might have.
    The different types are:
    c:\user\myfile.dat
    testfile.dat
    data
    c:\thisfile

    Just call the function StripFileName with your file name. The parsed name is returned in NewFileName.
    ****/

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*************** PROTOTYPES ************************/
    
    static void StripFileName(char *FileName, char *NewFileName);
    
    /**************************************************/
    
    int main(int argc, char* argv[])
    {
    	
    	char FileName[31];
    	char NewFileName[31];
    
    	strcpy(FileName, "c:\\user\\myfile.dat");
    	StripFileName(FileName, NewFileName);
    	printf("(%s)\n", NewFileName);
    
    	strcpy(FileName, "testfile.dat");
    	StripFileName(FileName, NewFileName);
    	printf("(%s)\n", NewFileName);
    
    	strcpy(FileName, "data");
    	StripFileName(FileName, NewFileName);
    	printf("(%s)\n", NewFileName);
    
    	strcpy(FileName, "c:\\thisfile");
    	StripFileName(FileName, NewFileName);
    	printf("(%s)\n", NewFileName);
    
    	return 0;
    }
    
    static void
    StripFileName(char *FileName, char *NewFileName)
    {
    	int PLocation;
    	int i;
    	int x;
    
    	i = strlen(FileName) - 1;
    
    	/* LOOK FOR THE PERIOD OR THE BEGINNING OF THE FILE NAME */
    	while (i && FileName[i] != '.')
    	{
    		i--;
    	}
    
    	/* IF I IS > 0, THE FILE NAME HAS A PERIOD */
    	if (i)
    	{
    		PLocation = i;
    	}
    	else
    	{
    		/* NO PERIOD, SO COPY TO THE END OF THE FILE NAME */
    		PLocation = strlen(FileName);
    
    		/* RESET i TO LOOK FOR THE FIRST PATH NAME SEPARATOR, SINCE i WENT TO ZERO LOOKING FOR A PERIOD */
    		i = strlen(FileName) - 1;
    	}
    
    	/* SEARCH FOR THE FIRST PATH NAME SEPARATOR, IF THERE IS ONE */
    	while (i && FileName[i] != '\\')
    	{
    		if (FileName[i] != '\\')
    			i--;
    	}
    
    	/* IF THERE IS A SEPARATOR, WE NEED TO GO FORWARD 1 CHARACTER */
    	if (FileName[i] == '\\')
    		i++;
    		
    	x = 0;
    
    	/* COPY FROM THE SEPARATOR (OR THE BEGINNING OF THE FILE NAME IF NO SEPARATOR) TO LOCATION OF THE PERIOD */
    	while (i < PLocation)
    	{
    		NewFileName[x] = FileName[i];
    		x++;
    		i++;
    	}
    	/* PLACE A NULL AT THE END WHEN WE'RE DONE COPYING */
    	NewFileName[x] = '\0';
    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Thaaaaaanks a lot for support

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21