【问题标题】:pass to a function the path of a directory,C language将目录的路径传递给函数,C语言
【发布时间】:2020-09-02 16:59:12
【问题描述】:

这是我的代码,理论上一旦从终端获取搜索和文件扩展名应该查找其中包含的这些文件并打印它们的最后修改和绝对路径的目录......问题出在路径打印.. ..例如/home/ciccio/prova.txt ....而不是...... /home/ciccio/dir/subdir/prova.txt 这是因为我只将文件名传递给“realpath” ricor1()中包含的函数,它从工作目录开始并解析过去的路径..所以我认为我应该通过目录的绝对路径来解决问题......或者建议我应该怎么做

#include<stdio.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdlib.h> 
#include<dirent.h>    
#include<stdarg.h>
#include<limits.h>
#include<string.h>
#include<time.h>
char * percorso;

////////////
char * scrivi(const char * a, char * b)
{

char *targetdir = malloc(2048);
strcpy(targetdir,a);
strcat(targetdir,"/");
strcat(targetdir,b);

//printf("%s \n", targetdir); 
return targetdir;
} 
//////////////


void ricor1(const char estensione[],const char nomedirectory[]){

struct stat attr;
char dbuf[PATH_MAX+1];
DIR * fh ;//puntatore ad una struttura DIR


struct dirent *fdata;

struct stat buf;
if((fh=opendir(nomedirectory))==NULL){
perror("ERRORE 1");
exit(errno);
}


while((fdata = readdir (fh))!=NULL){

// se è una directory richiama ricorsivamente

if(strcmp(fdata->d_name,".")==0)
{
  continue;
}
if(strcmp(fdata->d_name,"..") ==0)
{
continue;
}
if(fdata->d_type==DT_DIR)
{
 { percorso=scrivi(nomedirectory,fdata->d_name);
   ricor1(estensione,percorso);
 }
}

//
//


if(strstr(fdata->d_name,estensione)){

realpath(fdata->d_name,dbuf);

printf("[%s]",dbuf);

stat(nomedirectory,&attr);

printf("%s\n",ctime(&attr.st_mtime));
}

}

}      


void ricor2(const char estensione[]){

struct stat attr;
char dbuf[PATH_MAX+1];
DIR * fh ;//puntatore ad una struttura DIR
struct dirent *fdata;
struct stat buf;

if((fh=opendir("./"))==NULL){
perror("ERRORE 1");
exit(errno);
}


while((fdata = readdir (fh))!=NULL){

if(strstr(fdata->d_name,estensione)){

realpath(fdata->d_name,dbuf);

printf("[%s]",dbuf);

stat("./",&attr);

printf("%s\n",ctime(&attr.st_mtime));
}

}

}




int main(int argc, char *argv[])
{



  if(argc==3){

            printf("Controllo esistenza directory.. \n");

             DIR* dir=opendir(argv[2]);

          if(dir){
                   ricor1(argv[1],argv[2]);
    } else if (ENOENT==errno){

        printf("La directory passata non esiste");
  }
   else{

   printf("altro errore"); }   }


else if(argc==2){


ricor2(argv[1]);

}
}

【问题讨论】:

  • 您的问题不清楚。您的意思是是否可以将命令行参数作为函数的参数传递给程序?在那种情况下:是的。 How:这在每个基本的 C 教程中都有。请提供您要解决的实际问题的描述,并附上您编写的代码,以便我们了解您的位置和需求。
  • 好的,我试着更好地解释自己......我必须将返回目录路径的命令传递给函数,这可能吗?
  • 你没有更清楚。

标签: c directory path


【解决方案1】:

就像 Cheatah 说的,你需要向我们提供更多细节,比如,我们甚至不知道你的这个函数是做什么的,但一般来说,在 C 和 C++ 中,你需要使用正斜杠“/”作为与指定文件路径时的反斜杠相反,您还需要确定是使用绝对路径(例如:“D:/images/img.png”)还是相对路径,如果您访问的目录是在项目文件夹本身(示例:“images/img.png”)中,而不是(“D:/Projects/c_project/images/img.png”),其中“D:/Projects/c_project”是您的项目文件夹,“ images" 是您尝试访问的目录。

简而言之,您的代码如下所示:

function f(string s){

int main(){

f("D:/images/img.png");//Absolute path
f("images/img.png");//Relative path

}

function f(string s){...}

希望我正确理解了您的问题,如果没有,您需要提供更多详细信息。

【讨论】:

  • 这是 C++,但问题标记为 C
  • 上次我检查时在 c 和 c++ 中声明和调用函数是一样的。
  • functionstring 都会导致 C 中的编译错误。
  • 当然会,因为这是伪代码,我假设 OP 知道足以将其翻译成 C,因为在 C 函数中应该具有类似“int f”或“string”的类型f"(如果没有返回类型,则为 "void f"),你也应该调用 头文件,这就是为什么 'string' 给你一个错误。
  • C 中不存在 &lt;string&gt; 标头;这是 C++ 标准库的一部分。 &lt;string.h&gt; 是一个 C 头文件,其中没有声明这样的 string 类型。此外,假设 OP 知道不复制/粘贴您提供的代码有点幼稚,因为该网站上的许多人都是绝对的初学者,可能不像您知道的那么多,尤其是在两种语言之间进行翻译时。
猜你喜欢
  • 2016-12-23
  • 1970-01-01
  • 2014-05-25
  • 2011-02-26
  • 2021-12-02
  • 2018-12-26
  • 2010-10-08
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多