【问题标题】:C - Obtaining directory pathC - 获取目录路径
【发布时间】:2015-10-05 10:44:20
【问题描述】:

我正在编写一个程序,当它在一个目录中执行时会生成一个包含该目录中所有内容的文本文件。我正在获取从 **argv 到 main 的目录路径,因为我使用的是 netbeans 和 cygwin,所以我必须在我的 char* get_path(char **argv) 函数中对获得的路径进行一些字符串操作。目录路径大小总是会有所不同,因此我使用 malloc 分配空间以将其存储在内存中。

程序sn-p:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include "dbuffer.h"  //my own library
#include "darray.h"   //my own library

ARR* get_dir_contents( char* path)
    {
    DBUFF *buff = NULL;
    ARR *car = NULL;   
    DIR *dir_stream = NULL;
    struct dirent *entry = NULL;

    dir_stream = opendir(path);
    if(opendir(path)==NULL) printf("NULL");

    //... more code here 
    return car;
    }

char* get_path(char **argv)
    {
    char *path = malloc(sizeof(char)* sizeof_pArray( &argv[0][11] ) + 3 );

    strcpy(path, "C:");
    strcat(path, &argv[0][11]);

    printf("%s, sizeof: %d \n",path, sizeof_pArray(path));
    return path;
    }

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

    char *p = get_path(argv);

    ARR *car = get_dir_contents(&p[0]);

    //... more code here 

    return (EXIT_SUCCESS);
    }

问题是我拥有的字符串没有初始化dir_stream 指针。我怀疑这是因为指针和字符串文字之间存在一些差异,但我无法确定它到底是什么。此外,dirent 库函数需要 DIR *opendir(const char *dirname); const char 的事实可能与它有关。

输出:

C:/Users/uk676643/Documents/NetBeansProjects/__OpenDirectoryAndListFiles/dist/Debug/Cygwin_4.x-Windows/__opendirectoryandlistfiles, sizeof: 131 
NULL
RUN FAILED (exit value -1,073,741,819, total time: 2s)

【问题讨论】:

  • sizeof_pArray 听起来不对,为什么不使用标准函数 strlen ?也不需要将** 传递给get_path,只需将其声明为普通char* 并传递*argv 以使其更简单。
  • @Cyber​​Spock 这是我自己的函数,它获取字符串长度 + 终止字符
  • 如果opendir() 失败而不是printf("NULL") 执行perror("opendir() failed") 并且可能会明白出了什么问题。
  • 如果你的程序是为 cygwin 构建的,你不应该尝试“本地化”路径。照原样使用它们。 opendir 将在 "/cygwin/c/Users/blah" 上工作。

标签: c arrays string pointers malloc


【解决方案1】:

这里有些事情可能会出错,所以 我建议改为这样做

char* get_path(char *argv)
{
  char *path = malloc(sizeof(char)* strlen(argv) ); 

  if (path != NULL)
  {
    strcpy(path, "C:");
    strcat(path, argv + 11);

    printf("%s, sizeof: %d \n",path, strlen(path));
  }
  return path;
}


...
char* p = get_path(*argv);

注意:您不需要额外的 3 个字节,因为您分配的内容包括稍后跳过的 11 个字节。尽管您可能希望分解字符串,然后再将其放在一起以使其具有可移植性,而不是偏移 11 个字节。例如。使用strtok,您可以拆分该路径并替换您不需要的部分。

【讨论】:

  • 我刚刚尝试了你的建议,我的问题仍然存在
  • @ShadyProgrammer 上面的代码应该处理任何指针错误,但也许你的路径格式错误。调用opendir后检查errno判断错误。
  • 原来我的指针是正确的,但我得到的路径包含文件名本身,我不得不去掉路径的最后一位。我已经使用您建议的使用 strtok() 的方法完成了它。现在程序正在按照我的意愿运行。感谢您的帮助
【解决方案2】:

这可能是对 argv 的简单混淆吗?请插入以下行 就在你的 main() 开头,是你所期望的吗?

printf("\n argv[0]== %s" , argv[0]  );
getchar();

printf("\n argv[1]== %s" , argv[1]  );
getchar();

好的,所以我们从 argv[0] 开始工作,请为 get_path 试试这个

char *get_path(char *argv)
{
int i=0;
// +2 to add the drive letter
char *path = malloc(sizeof(char)* strlen(argv)+2 );

if (path != NULL)
{
 strcpy(path, "C:");
 strcat(path, argv);

 // we get the path and the name of calling program
 printf("\n  path and program== %s",path);
 printf("%s, sizeof: %d \n",path, strlen(path));
 // now remove calling program name
   for( i=strlen(path) ; ; i--)
   {
     // we are working in windows
      if(path[i]=='\\') break;
      path[i]='\0';
   }

 }
 return path;
}

【讨论】:

  • 感谢您的回答,但我确定没有混淆,而且我已验证检查:argv[0]== /cygdrive/c/Users/uk676643/Documents/NetBeansProjects/__OpenDirectoryAndListFiles/dist/Debug/Cygwin_4.x-Windows/__opendirectoryandlistfiles argv[1]== (null)
  • 事实证明 argv[0] 为我提供了我不知道但能够摆脱文件名的路径和文件名。 +1 // we get the path and the name of calling program 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 2019-02-18
  • 2016-05-27
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多