简体   繁体   English

将文件字节写入文件客户端 C++

[英]writing file bytes to a file client side c++

I have a server (python) that sends bytes from a file to a client in c++.我有一个服务器(python),它在 C++ 中将字节从文件发送到客户端。 I am using libcurl to make requests to the python server and flask to do all of the "hard" work for me in python.我正在使用 libcurl 向 python 服务器和烧瓶发出请求,以便在 python 中为我完成所有“艰苦”的工作。 After i get the file bytes from the server, i want to write it to a zip file on the client side.从服务器获取文件字节后,我想将其写入客户端的 zip 文件。 Initially, i was going to use libcurl to do it for me, but i decided i didn't want to do that as it would require an extra function in my wrapper which is not necessary.最初,我打算使用 libcurl 为我做这件事,但我决定我不想这样做,因为它需要在我的包装器中添加一个不必要的额外功能。

FILE* zip_file = fopen(zip_name, "wb");
//make request and store the bytes from the server in a string
fwrite(response_information.first.c_str(), sizeof(char), sizeof(response_information.first.c_str()), zip_file);
//response_information is a pair . First = std::string, Second = curl response code

I do plan on switching to fopen_s (safe version of fopen), but i want to get a working program first.我确实计划切换到 fopen_s(fopen 的安全版本),但我想先获得一个工作程序。 This is part of a bigger project so i can't provide code that can be run.这是一个更大项目的一部分,所以我无法提供可以运行的代码。 Some things to note that i think can be causing this: storing response as string then attempting to get the c string version and write it to the file.我认为可能导致这种情况的一些注意事项:将响应存储为字符串,然后尝试获取 c 字符串版本并将其写入文件。 When storing the return value/code of fwrite, i get "8" which means "*" bytes written apparently.在存储 fwrite 的返回值/代码时,我得到“8”,这意味着显然写入了“*”字节。 Also, when im on windows, it says that the file was modified after i run my program, but nothing is in the zip file itself.此外,当我在 Windows 上时,它说我运行我的程序后文件被修改,但 zip 文件本身没有任何内容。 How can i write the response bytes to a file?如何将响应字节写入文件?

The third parameter in fwrite is a count of items to write. fwrite的第三个参数是要写入的项目数。 So sizeof doesn't seem to be the thing you need.所以sizeof似乎不是你需要的东西。 response_information.first.c_str() is a pointer, so sizeof(response_information.first.c_str()) returns a pointer size. response_information.first.c_str()是一个指针,所以sizeof(response_information.first.c_str())返回一个指针大小。 Here it should be:这里应该是:

fwrite(response_information.first.c_str(), sizeof(char), strlen(response_information.first.c_str()), zip_file);

or或者

fwrite(response_information.first.c_str(), sizeof(char), response_information.first.length(), zip_file);

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

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