How to retieve files from a folder and pass it in a functio called in a loop
-
Hi, I have an array of strings which contains the file names and I call a function in a loop.
char myFiles [20][50];
for(i=0;i<20;i++)
myFunc( myFiles[i]);The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call
myFunc( myFiles[i]);
which I am unable to do. I used
mkdir("input");
to create a directory and move all my files into it also using
system("move *gen.txt input");
But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie
for(i=0;i<=20;i++)
myFunc("C:\\input\\myFiles[i]"); //which is not possible....I am using eclipse IDE. Thanks in advance, Faez
-
Hi, I have an array of strings which contains the file names and I call a function in a loop.
char myFiles [20][50];
for(i=0;i<20;i++)
myFunc( myFiles[i]);The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call
myFunc( myFiles[i]);
which I am unable to do. I used
mkdir("input");
to create a directory and move all my files into it also using
system("move *gen.txt input");
But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie
for(i=0;i<=20;i++)
myFunc("C:\\input\\myFiles[i]"); //which is not possible....I am using eclipse IDE. Thanks in advance, Faez
Have a look at the
str...
functions. To create a full file name, you may usestrcpy
andstrcat
:char PathName[_MAX_PATH]; // _MAX_PATH is defined in stdlib.h
for (i = 0; <= 20; i++)
{
strcpy(PathName, "C:\\input\\");
strcat(PathName, myFiles[i]);
myFunc(PathName);
} -
Hi, I have an array of strings which contains the file names and I call a function in a loop.
char myFiles [20][50];
for(i=0;i<20;i++)
myFunc( myFiles[i]);The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call
myFunc( myFiles[i]);
which I am unable to do. I used
mkdir("input");
to create a directory and move all my files into it also using
system("move *gen.txt input");
But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie
for(i=0;i<=20;i++)
myFunc("C:\\input\\myFiles[i]"); //which is not possible....I am using eclipse IDE. Thanks in advance, Faez
It sounds to me like you need to concatenate the filename onto the foldername. //In that case, you may choose to do something similar to this: // 1. Get the filenames // 2. Get the folder name // 3. copy foldername to fullPath // 4. concatenate (append) myFiles[i] onto fullPath // 5. If curFileNum < maxFiles goto 3.
char filenames[4][50] = { "001.txt", "002.txt", "003.txt", "004.txt" };
char inputFolder[] = "inputFolder";
int i, maxFiles=4;`for (i=0; i<maxFiles; i++)
{
strcpy(fullPath, inputFolder); // fullPath = "inputFolder"
strcat(fullPath, "\\"); // fullPath = "inputFolder\"
strcat(fullPath, myFiles[i]); // fullPath = "inputFolder\001.txt" - first iteration only
myFunc(fullPath); // myFunc("inputFolder\001.txt");
}[EDIT: oops! Too slow - someone has kindly furnished an answer already]
-
Have a look at the
str...
functions. To create a full file name, you may usestrcpy
andstrcat
:char PathName[_MAX_PATH]; // _MAX_PATH is defined in stdlib.h
for (i = 0; <= 20; i++)
{
strcpy(PathName, "C:\\input\\");
strcat(PathName, myFiles[i]);
myFunc(PathName);
}That worked :-) BTW I came up another method as below
system("copy input/*.txt "); // This line copies all the text files to pwd
/* After manipulations with all the required files.... */
system("del *.txt");
Anyways, thanks for the nice logic :) Regards, Faez