执行malloc后将字符串传递给函数

家伙

这是我主要的C函数中的代码。

char *filename;            

filename = malloc(2001); //if I don't do this step it gives me an error

filename = ListDirectoryContents("test_data\\tmp");//this returns a string  for filename

printf("filename : %s ", filename);//value of filename is displayed which is an address and is NOT NULL OR EMPTY

copyObjectFileInINfolder(filename, logger);//here I am passing the filename to this function where I am getting the error

好的,现在我要尝试将字符串文件名传递到函数中(在函数中,文件名的变量是对象)

void copyObjectFileInINfolder(char *object, char* logger2);
{
        char source[500];
        char destination[500];
        char cmd[1000];
        printf("\nobject : 123%s123\n", object);//Here the out put I see is "object : 123123"
        printf("\nlogger : %s\n", logger2);

         if(flag ==2)//for windows
         {      
                  //object +=14;
                  printf("\nobject : %s\n", object););//Here the out put I see is "object : "

                  sprintf(source, "test_data\\tmp\\%s", object ); //full address of source file
                  sprintf(destination, "..\\in\\%s", object ); //full address of where the file is to be copied


                  sprintf(cmd,"IF EXIST %s del /Q %s", destination, destination);//to make sure to delete the file if it already exists in the safeXhonw\in folder
                  system(cmd);
                  memset(cmd,0,strlen(cmd));//deleting old values stored in cmd

                  sprintf(cmd, "copy %s %s", source, destination );
         }

        system(cmd);//using linux terminal or Windows command prompt to copy file from source folder into safeXhomedirectory in folder             

        memset(cmd,0,strlen(cmd));//deleting old values stored in cmd

        memset(source,0,strlen(source));//deleting old values stored in source
        memset(destination,0,strlen(destination));//deleting old values stored in destination

}

但是,当我通过它时,它始终为空,即文件名/对象不显示任何值,甚至不显示“(空)”。我知道这可能与此malloc命令有关。

这里是其他功能

char* ListDirectoryContents(const char *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;

char sPath[2000];

//Specify a file mask. *.* = We want everything!
sprintf(sPath, "%s\\*.*", sDir);

if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
    printf("Path not found: [%s]\n", sDir);
    return 1;
}

do
{
    //Find first file will always return "."
    //    and ".." as the first two directories.
    if(strcmp(fdFile.cFileName, ".") != 0
            && strcmp(fdFile.cFileName, "..") != 0)
    {
        //Build up our file path using the passed in
        //  [sDir] and the file/foldername we just found:
        sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName);

        //Is the entity a File or Folder?
        if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
        {
            printf("Directory: %s\n", sPath);
            ListDirectoryContents(sPath); //Recursion, I love it!
        }
        else{
            printf("File: %s\n", sPath);
            FindClose(hFind);
            return sPath;
        }
    }
   }while(FindNextFile(hFind, &fdFile)); //Find the next file.

FindClose(hFind); //Always, Always, clean things up!

    return "";
}
一些程序员哥们

您有两个问题:第一个很明显,那就是重新分配,filename使您失去原始指针。

另一个问题是在ListDirectoryContents函数中您将返回一个指向局部变量的指针sPath,这将导致未定义的行为,因为一旦函数返回该变量就不存在了。

我可以看到两种可能的解决方案:

  1. filenameas作为第二个参数传递给ListDirectoryContents函数,然后使用该参数代替sPath
  2. 不要为分配内存filename,而是sPathListDirectoryContents函数中动态分配

另外,无论您对问题采取什么措施,函数以某种方式失败时通常要做的事情就是不返回指向有效但为空的字符串的指针。相反,通常会返回NULL

哦,顺便说一句,in的递归ListDirectoryContents将不会真正按照您的期望工作,因为您不处理返回的值,而是将其丢弃。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将字符串传递给函数

来自分类Dev

将字符串传递给函数

来自分类Dev

将字符串传递给 init 函数并将其存储在 malloc 的结构中

来自分类Dev

如何将双引号字符串传递给作为 sudo 执行的函数

来自分类Dev

将字符串传递给javascript函数

来自分类Dev

无法将字符串传递给函数

来自分类Dev

将字符串数组传递给C中的函数

来自分类Dev

将C ++字符串传递给ac#函数

来自分类Dev

将字符串矩阵传递给修改它的函数

来自分类Dev

如何将字符串传递给宏函数

来自分类Dev

将字符串文字传递给模板函数

来自分类Dev

将HTML字符串传递给JavaScript函数

来自分类Dev

将字符串传递给ggplot函数

来自分类Dev

C ++将字符串数组传递给函数

来自分类Dev

将字符串传递给接受char指针的函数

来自分类Dev

将编码的字符串传递给Javascript函数

来自分类Dev

将字符串作为键参数传递给函数

来自分类Dev

将指针(字符串)传递给C函数

来自分类Dev

将空字符串传递给构造函数

来自分类Dev

将默认的字符串指针传递给函数

来自分类Dev

将空格字符串作为参数传递给函数

来自分类Dev

将参数传递给函数以返回字符串

来自分类Dev

如何将字符串数组传递给函数?

来自分类Dev

Arduino:将函数传递给类,返回字符串

来自分类Dev

C 将字符串传递给函数问题

来自分类Dev

将 Ruby 字符串传递给 Javascript 函数参数

来自分类Dev

将字符串传递给回调函数

来自分类Dev

将连接字符串传递给 ViewModel 构造函数

来自分类Dev

将路径字符串传递给 jquery 函数