Solved

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

Posted on 2004-04-13
19
421 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:ico2
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 1

Expert Comment

by:ravenscr98
ID: 10818225
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.
0
 
LVL 3

Expert Comment

by:idt
ID: 10819493
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
 
 
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 20 total points
ID: 10819916
For DOS,i.e. TurboC compilers

/* findfirst and findnext example */

#include <stdio.h>
#include <dir.h>

int main(void)
{
   struct ffblk ffblk;
   char *ptr,*ptr1;
   int done;
   printf("Directory listing of *.*\n");
   done = findfirst("*.*",&ffblk,0);
   while (!done)
   {
//this contains the entire name: printf("  %s\n", ffblk.ff_name);
    ptr=strtok(ffblk.ff_name,".");//ptr contains the first name.
    printf("%s",ptr);
    ptr1=strtok(NULL,"\0");//ptr1 contains the extension
    printf("%s",ptr1);
    done = findnext(&ffblk);
   }

   return 0;
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10819927
Here You will have to check for directories and files which dont have an extension.
0
 
LVL 3

Expert Comment

by:idt
ID: 10820295
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
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10820474
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

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10820502
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.
0
 
LVL 3

Expert Comment

by:idt
ID: 10820555
I didn't realize this was a 'dos' question ?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10821787
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. :~))


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:idt
ID: 10822775
hahahaha... an Ace in the hole!
0
 
LVL 1

Author Comment

by:ico2
ID: 10826186
thanks ankuratvb.
also thanks for the tip on arrays.
0
 
LVL 1

Author Comment

by:ico2
ID: 10826519
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.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10829533
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
0
 
LVL 1

Author Comment

by:ico2
ID: 10831622
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
  }
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10832638

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++;
 }

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10832647
If this doesnt work,post the entire code so that we can check whether the strings are being stored properly or not.
0
 
LVL 1

Author Comment

by:ico2
ID: 10834186
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++;
 }
}
0
 
LVL 1

Author Comment

by:ico2
ID: 10834188
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++;
 }
}
0
 
LVL 1

Author Comment

by:ico2
ID: 10834575
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now