Thread: Read files without extension

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12

    Read files without extension

    Hi, I need some help with a little program.

    Code:
    #include <stdio.h>
    #include <dirent.h>
    main()
    {
    	  DIR *d;
     	  struct dirent *dir;
     	  char c, *name[100];
     	  int i, cont=0;
    
    	  d = opendir ("C:/Test"); 
    	  for (i=0; ((dir = readdir (d))!= NULL); i++)
    	  	  	{
       		  	name[i] = strdup (dir-> d_name);
       		  	cont ++;						 
       		  	printf ("C:\\Test\\%s\n", name[i]);
    		  	}
    	  closedir (d); 
    	  system ("pause");
    }
    The main problem, is that that PATH is full of .zip files,
    Example File.zip, File2.zip

    and I need to create the array, without de .zip extension, so the array would be with the names only,

    File, File 2 .... etc...

    Is there a way to do that??

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    There are several methods. If you know for sure that your files will always have an extension, you can do a sscanf on each to extract the parts on either side of the period. You could also go through the string and get the index of the period, and then splice the string from 0 to that point.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you don't care about the extensions, you could just blast them out with the usage of a helper function.

    Code:
    void eraseExtension(char *name)
    {
        char *p = strchr(name, '.');
    
        if(p != NULL)
        {
            *p = '\0';
        }
    }
    Might need to be careful about passing it filenames of "." or "..", but the idea is what counts.

    This is not ideal if you actually need to keep the extensions at a later point, though.

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12
    Thanks!!

  5. #5
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    You could try name[i]=strtok(name[i],".");
    But this will work only if names of your files are without points except for the one of the extension.
    Another way: name[i][strlen(name[i])-4]='\0';
    And this will work only if the extension is formed by 3 letters.

    PS: to use strdup you should include string.h

    PPS: sorry for my english

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MacGyver View Post
    Might need to be careful about passing it filenames of "." or "..", but the idea is what counts.

    This is not ideal if you actually need to keep the extensions at a later point, though.
    You might also have to consider file names with more than one . such as: foo.bar.baz, in which case strrchr would be preferred.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ZaC View Post
    You could try name[i]=strtok(name[i],".");
    But this will work only if names of your files are without points except for the one of the extension.
    Another way: name[i][strlen(name[i])-4]='\0';
    And this will work only if the extension is formed by 3 letters.

    PS: to use strdup you should include string.h

    PPS: sorry for my english
    Acutally, strdup() is not a standard function, so including string.h will not guarantee that it is there. For example MS compilers do not include this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Quote Originally Posted by matsp View Post
    Acutally, strdup() is not a standard function, so including string.h will not guarantee that it is there. For example MS compilers do not include this.

    --
    Mats
    Good to know... so the standard way should be using memcpy or strncpy, isn't it? Which is better? Why?
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For a quick implementation to duplicate a string, I would probably opt for malloc + strcpy since it's so straightforward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open and read bmp image files
    By cnewbie85 in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 01:36 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Replies: 3
    Last Post: 05-05-2004, 05:40 PM
  5. Find all files with extension in a directory
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 12-17-2001, 01:45 AM

Tags for this Thread