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

时间:2015-11-12 12:16:12

标签: c

这是我在主要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

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

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

}

但是当我传递它时,它总是空的,即文件名/对象不显示任何值,甚至不显示“(null)”。我知道这可能与这个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 "";
}

1 个答案:

答案 0 :(得分:4)

你有两个问题:第一个是非常明显的,并且filename的重新分配使你失去原始指针。

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

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

  1. filename作为第二个参数传递给ListDirectoryContents函数,并使用该参数代替sPath
  2. 不要为filename分配内存,而是在sPath函数中动态分配ListDirectoryContents
  3. 此外,无论你对你的问题做了什么,当函数以某种方式失败时常见的事情是不返回指向有效但空的字符串的指针。相反,通常会返回NULL

    哦顺便说一下,ListDirectoryContents中的递归确实没有按照您的预期发挥作用,因为您没有处理返回的值,您只需丢弃它。