Link to home
Start Free TrialLog in
Avatar of ico2
ico2

asked on

newbie ?: how to get files and their extensions into seperate variables

how do i do this? i want to do it so that if the argument sent to it is one then it gets the first 20 files and their extensions and puts them in the variables filename1,filename2...filename20 and fileext1,fileext2...fileext20 and it the argument is 2 then display the next 20 files put in the same variables.
and obviosly if there are only 15 files then leave the last 5 blank.
someone game me the code to display the contents of a folder but i cannot seem to adapt it to this.
Avatar of ravenscr98
ravenscr98

Is this on windows or a flavor of unix?  On unix there may not be an extension.

Look at the opendir, dirent, and closedir functions for reading the files in a directory.  The stat function will give you information about a file, including its type (link, regular, directory, etc.).  As you use dirent to get the file names, you can parse them into their name and extension.
On windows there may not be an extension either..
And if the file has muliple '.'s..

probably something like (warning psuedo code, not optimized, not compilable)


struct {
  (*NIX) int unixhiddenfile;
  char filename[MAXLEN];
  char extension[MAXEXT];
} filenamepartstype;

filenamepartstype filenameparts[20]={0};
int count=0;

For each file count++
for ( int i=(strlen(myFile)-1);i>-1,i--) if(myFile[i]=='.') break;     //work backwards since you want the last '.'

IF *NIX:
if ((i==0) && (myFile[i]=='.')) filenameparts[count].unixhiddenfile=1;
if ((i==0) strncpy(filenameparts[count].filename,myFile,MAXLEN);
else {
  strncpy(filenameparts[count].filename,myFile,i);
  strncpy(filenameparts[count].extension,myFile+(i+1),MAXEXT);
}

IF WIN:
if ((i==0) strncpy(filenameparts[count].filename,myFile,MAXLEN);
else {
  strncpy(filenameparts[count].filename,myFile,i);
  strncpy(filenameparts[count].extension,myFile+(i+1),MAXEXT);
}

Daniel
 
 
ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here You will have to check for directories and files which dont have an extension.
if the filename ="this.is.a.file.txt" ?

is why I used the for loop i--;

moreover, strtok is no more efficient than a for loop

Daniel
Hi daniel,

Yes,you are right but in DOS,such filenames are not allowed in that they are not stored as is.Dos trims the filename to its accepted format,something like thisis~1.txt

So there will only be 1 . in the filename

Hi ico2,

Since you want to store only 20 files and extensions in dif. variables,you can consider using an array.

char filname[20][9];//primary filename 8 chars plus one '\0'
char ext[20][4];//extension 3 chars

run a counter inside the loop and do:
strcpy(filname[i],ptr);
strcpy(ext[i],ptr1);
increment i.
I didn't realize this was a 'dos' question ?
Hi daniel,

>someone game me the code to display the contents of a folder but i cannot seem to >adapt it to this.

That someone just happened to be me. :~))


hahahaha... an Ace in the hole!
Avatar of ico2

ASKER

thanks ankuratvb.
also thanks for the tip on arrays.
Avatar of ico2

ASKER

one prob though: when i try and display the data again from the arrays i get what seems to be the entirety of the ms-dos help files displayed.

here is the code i am using to display it:
   printf("\n\aheres the array version\n");
   system("pause");
   loopvar=0;
   i=0;
   while(loopvar==0){
   printf("\n%s%s",filename[i],ext[i]);
   ++i;
   }

there is probably a mistake i made with the above code.
printf("\n\aheres the array version\n");
  system("pause");
  loopvar=0;
  i=0;
  while(loopvar==0){
  printf("\n%s%s",filename[i],ext[i]);
  ++i;
  }

Where is the termination condition for the while loop?Since loopvar is always zero,it runs in an infinite loop.

Put a termination condition in the while loop
Avatar of ico2

ASKER

ok here's the code i now use but it still produces some wierd filenames:

   printf("\n\aheres the array version\n");
i=0;
   while(loopvar){
printf("\nfile: %s%s",filname[i],ext[i]);
if(i==20)loopvar=0
  }

Here you are not incrementing 'i'

DO this:

printf("\n\aheres the array version\n");

i=0;
while(i<=20){//to read 20 names you need to change this to i<20 since 0 to 19 is 20 names
     printf("\nfile: %s%s",filname[i],ext[i]);
     i++;
 }

If this doesnt work,post the entire code so that we can check whether the strings are being stored properly or not.
Avatar of ico2

ASKER

ok i used the code you suggested and it woks fine except that the first filename shows up wrong in the array version.

#include <stdio.h>
#include <dir.h>
#include <string.h>
char filname[20][9];
char ext[20][4];
int main(void)
{
int i=0;
int loopvar;
   struct ffblk ffblk;
   char *ptr,*ptr1;
   int done;
   system("cd c:\\tc\\");
   printf("Directory listing of *.*\n");
   done = findfirst("*.*",&ffblk,0);
   while (!done)
   {
    ptr=strtok(ffblk.ff_name,".");
    ptr1=strtok(NULL,"\0");
    printf("\n%s.%s",ptr,ptr1);
    strcpy(filname[i],ptr);
    strcpy(ext[i],ptr1);
    done = findnext(&ffblk);
    if(i==20)break;
++i;
   }

  printf("\n\aheres the array version\n");
i=0;
while(i<=20){
     printf("\nfile: %s.%s",filname[i],ext[i]);
     i++;
 }
}
Avatar of ico2

ASKER

ok i used the code you suggested and it woks fine except that the first filename shows up wrong in the array version.

#include <stdio.h>
#include <dir.h>
#include <string.h>
char filname[20][9];
char ext[20][4];
int main(void)
{
int i=0;
int loopvar;
   struct ffblk ffblk;
   char *ptr,*ptr1;
   int done;
   system("cd c:\\tc\\");
   printf("Directory listing of *.*\n");
   done = findfirst("*.*",&ffblk,0);
   while (!done)
   {
    ptr=strtok(ffblk.ff_name,".");
    ptr1=strtok(NULL,"\0");
    printf("\n%s.%s",ptr,ptr1);
    strcpy(filname[i],ptr);
    strcpy(ext[i],ptr1);
    done = findnext(&ffblk);
    if(i==20)break;
++i;
   }

  printf("\n\aheres the array version\n");
i=0;
while(i<=20){
     printf("\nfile: %s.%s",filname[i],ext[i]);
     i++;
 }
}
Avatar of ico2

ASKER

i discovered that it was mixing the last extension with the first filename. i fixed this by making it write and read from 1 to 21.

thanks for all your help.