在C中动态创建文件名

在C中动态创建文件名,c,string,file,server,client,C,String,File,Server,Client,我在做一个客户端,服务器项目。我从服务器收到一个响应,在那里我必须创建两个文件。第一个文件是.html文件,第二个文件是.png文件。我解析响应并尝试在while循环中动态创建文件 ... FILE *fwrp; char *response = malloc(MAXDATASIZE * sizeof(char)); memset(response, '\0', sizeof(char) * MAXDATASIZE); char *r = &response[0]; while (f

我在做一个客户端,服务器项目。我从服务器收到一个响应,在那里我必须创建两个文件。第一个文件是.html文件,第二个文件是.png文件。我解析响应并尝试在
while
循环中动态创建文件

...
FILE *fwrp;
char *response = malloc(MAXDATASIZE * sizeof(char));
memset(response, '\0', sizeof(char) * MAXDATASIZE);
char *r = &response[0];


while (fread(field, sizeof(field), 1, fpr)) {
    for (int i = 0; i < MAXDATASIZE; i++) {
        *r = field[i];
        r++;


//Some unecessary code...

                    if (strstr(response,"file=")){
                    char* newFileName = strchr(response, '=');
                    newFileName++;
                    fwrp = fopen(newFileName, "w");
                    data = false;
//...  
因此,通过这种修改,问题得到了解决

 newFileName[strlen(newFileName) - 1] = 0;
  • 我不知道,你为什么要将读取的
    字段
    内容复制到
    响应
    中,而不是严格地读取到
    响应
    。另外,您没有显示
    字段的声明

  • 可能您应该更加注意读取字符的计数(由
    fread()
    返回):它可以是负数(表示错误),而不仅仅是正数或0

  • 零终止呢

  • 因此,我的建议是:

    //...
    // for better readability
    #define MAX_RESPONSE_SIZE (MAXDATASIZE * sizeof(char))
    
    // below: added 1 byte for preserving the proper string termination ('\0' byte)
    #define RESPONSE_BUFFER_SIZE (MAXDATASIZE + 1)
    
    FILE *fwrp;
    char *response = malloc(RESPONSE_BUFFER_SIZE);
    memset(response, '\0', RESPONSE_BUFFER_SIZE);
    
    int readOffset = 0; // offset for reading the next chunk
    int readCount; // count of readed characters/bytes
    
    // below: return of fread() can be positive, 0 if no more data, or negative if error
    while ((readCount = fread(response + readOffset, MAX_RESPONSE_SIZE - readOffset, 1, fpr)) > 0) {
        readOffset += readCount;
    }
    
    //Some unecessary code...
    
    if (strstr(response, "file=")) {
        char* newFileName = strchr(response, '=');
        newFileName++;
        fwrp = fopen(newFileName, "w");
        data = false;
    //...
    

    “反应”从何而来?很明显,您希望它看起来像“file=name”,而实际上它看起来像“file='name'…”。如果是后者,你必须自己解析。谢谢你的回答。回答看起来像这个file=example.htmli也编辑了我的问题。因此,您可以在调用fopen之前比较ok.png和newFileName指针内容之间的字符串,以查看响应来自何处。您在windows+VS、linux+gcc上使用的平台是什么?在调用fopen()之前,是否可以打印新文件名?如果最后有不需要的字符,在调用fopen()之前在那里重新处理可以解决问题,但它不能解释为什么您的代码会出现问题。thx。你说得对,你真的不需要这个领域。我之所以构建字段和响应,是因为字段是fread的“未完成响应”。因为fread不会一次给出服务器的全部响应。所以我从fread读取,直到命令告诉我服务器的整个响应都被读取。我的“response”变量一直构造到出现“\n”为止。所以我总是解析“响应行”,这使得我的解析逻辑更容易一些。但我会在上面发布我的解决方案
    //...
    // for better readability
    #define MAX_RESPONSE_SIZE (MAXDATASIZE * sizeof(char))
    
    // below: added 1 byte for preserving the proper string termination ('\0' byte)
    #define RESPONSE_BUFFER_SIZE (MAXDATASIZE + 1)
    
    FILE *fwrp;
    char *response = malloc(RESPONSE_BUFFER_SIZE);
    memset(response, '\0', RESPONSE_BUFFER_SIZE);
    
    int readOffset = 0; // offset for reading the next chunk
    int readCount; // count of readed characters/bytes
    
    // below: return of fread() can be positive, 0 if no more data, or negative if error
    while ((readCount = fread(response + readOffset, MAX_RESPONSE_SIZE - readOffset, 1, fpr)) > 0) {
        readOffset += readCount;
    }
    
    //Some unecessary code...
    
    if (strstr(response, "file=")) {
        char* newFileName = strchr(response, '=');
        newFileName++;
        fwrp = fopen(newFileName, "w");
        data = false;
    //...