如何在C程序中更改目录并从特定文件夹中检索文件 [英] How to change directory in C program and retrieve files from a particular folder
问题描述
我有一个包含文件名的字符串数组,并在循环中调用一个函数.
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]);
我面临的挑战是,我必须将所有文件存储在目录中,例如"input"文件夹....然后调用我无法执行的
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]);
.
我用了mkdir("input");创建目录并使用
which I am unable to do.
I used mkdir("input"); to create a directory and moves all my files into it also using
system("move *.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....
我正在使用Eclipse IDE.
预先感谢,
Faez
I am using eclipse IDE.
Thanks in advance,
Faez
推荐答案
您可以在循环中执行以下步骤
You can follow following steps in your loop
char FileName[1024] = {0};
for(i = 0; i<20;i++)
{
sprintf(FileName,"C:\\input\\myFiles[%d]",i);
myFunc(FileName);
}
这样,您便可以动态创建文件名.
In this way you are able to create you file name dynamically.
我使用了以下
I used the following
system("copy /input/*.txt ");
/* After manipulations with the file..*/
system("del *.txt);
这篇关于如何在C程序中更改目录并从特定文件夹中检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!