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...file name20 and fileext1,fileext2...fileex t20 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.
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.
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].unixh iddenfile= 1;
if ((i==0) strncpy(filenameparts[coun t].filenam e,myFile,M AXLEN);
else {
strncpy(filenameparts[coun t].filenam e,myFile,i );
strncpy(filenameparts[coun t].extensi on,myFile+ (i+1),MAXE XT);
}
IF WIN:
if ((i==0) strncpy(filenameparts[coun t].filenam e,myFile,M AXLEN);
else {
strncpy(filenameparts[coun t].filenam e,myFile,i );
strncpy(filenameparts[coun t].extensi on,myFile+ (i+1),MAXE XT);
}
Daniel
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,
IF *NIX:
if ((i==0) && (myFile[i]=='.')) filenameparts[count].unixh
if ((i==0) strncpy(filenameparts[coun
else {
strncpy(filenameparts[coun
strncpy(filenameparts[coun
}
IF WIN:
if ((i==0) strncpy(filenameparts[coun
else {
strncpy(filenameparts[coun
strncpy(filenameparts[coun
}
Daniel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.
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. :~))
>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!
ASKER
thanks ankuratvb.
also thanks for the tip on arrays.
also thanks for the tip on arrays.
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.
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
++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
system("pause");
loopvar=0;
i=0;
while(loopvar==0){
printf("\n%s%s",filename[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
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
}
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.
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++;
}
}
#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++;
}
}
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++;
}
}
#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++;
}
}
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.
thanks for all your help.
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.