简体   繁体   English

将文件名保存在C中的数组中

[英]Save filenames in array in C

I have this function: 我有这个功能:

void f_listfiles(char temp[200]){
    HANDLE hFind;
    WIN32_FIND_DATA FindData;
    int i;

    hFind = FindFirstFile("*", &FindData);

    while (FindNextFile(hFind, &FindData))
    {
    strcpy(temp, FindData.cFileName);

    }

    FindClose(hFind);
}

I want it to put the filenames in the directory in to char temp. 我希望它将目录中的文件名放入char temp中。 After that i am going to send it to a FTP client. 之后,我将其发送到FTP客户端。 How can i do this? 我怎样才能做到这一点? It currently overwrites each time and only the last file is in the char temp. 当前每次都会覆盖它,并且只有最后一个文件处于char temp中。

Edit:I cant use an array of pointers due to the fact that i later need to send this array with the function send(clientSocket, temp, sizeof(temp), 0) 编辑:我不能使用指针数组,因为我以后需要使用函数send(clientSocket,temp,sizeof(temp),0)发送该数组

I want it to put the filenames in the directory in to char temp[] 我希望它将目录中的文件名放入char temp[]

That is not going to work: a character array is a single string; 那是行不通的:字符数组是单个字符串; you need an array of strings. 您需要一个字符串数组。

That is, you need an array of pointers. 也就是说,您需要一个指针数组。 There are several ways of making it work. 有几种使它起作用的方法。 One is to let the caller pass an array, along with its length to avoid overruns, and then allocate strings dynamically as you go. 一种是让调用者传递一个数组及其长度,以避免溢出,然后随需动态分配字符串。 You need to return how many entries you filled in, otherwise the caller would not know where in his array the actual file names end. 您需要返回您填写的条目数,否则调用者将不知道实际文件名在数组中的何处结束。

size_t f_listfiles(char *names[], size_t max) {
    HANDLE hFind;
    WIN32_FIND_DATA FindData;
    size_t i = 0;
    hFind = FindFirstFile("*", &FindData);
    while (FindNextFile(hFind, &FindData) && i != max)
    {
        names[i] = malloc(strlen(FindData.cFileName)+1);
        strcpy(names[i], FindData.cFileName);
        i++;
    }
    FindClose(hFind);
    return i;
}

The caller would call your function like this: 调用者将这样调用您的函数:

char *names[200];
size_t count = f_listfiles(names, 200);
for (size_t i = 0 ; i != count ; i++) {
    printf("%02d: %s\n", i+1, names[i]);
}
// Caller needs to free dynamically allocated strings:
for (size_t i = 0 ; i != count ; i++) {
    free(names[i]);
}

I later need to send this array to a client 我以后需要将此数组发送给客户端

The code that sends this array would need to serialize it in some way - say, append strings one by one to a character buffer before sending. 发送此数组的代码将需要以某种方式对其进行序列化-例如,在发送之前,将字符串一个接一个地附加到字符缓冲区。

Besides the fact that temp maybe too small to handle or the strings you can do the following to overcome the overwrite problem: 除了温度可能太小而无法处理字符串之外,您还可以执行以下操作来克服覆盖问题:

int i = 0;
while (FindNextFile(hFind, &FindData))
{
    if(i == 0){
        strcpy(temp, FindData.cFileName);
        i++;
        continue;
    }

    strcat(temp, FindData.cFileName);
}

or 要么

temp[0] = 0; //terminating null character so strcat can join the strings
while (FindNextFile(hFind, &FindData))
{
    strcat(temp, FindData.cFileName);
}

There is a catch though. 不过有一个陷阱 The strcat will ovewrite the terminating null character of the temp string and append a new one in the end of the resulting string strcat将覆盖临时字符串的terminating null character ,并在结果字符串的末尾追加一个新的terminating null character

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM