保留文件字节的最合适的矢量类型是什么?
我正在考虑使用int类型,因为位"00000000"(1字节)被解释为0!
目标是将此数据(字节)保存到文件中,稍后从此文件中检索.
注意:文件包含空字节(以位为"00000000")!
我在这里有点失落.帮我!= D谢谢!
更新I:
要读取我正在使用此功能的文件:
char* readFileBytes(const char *name){
std::ifstream fl(name);
fl.seekg( 0, std::ios::end );
size_t len = fl.tellg();
char *ret = new char[len];
fl.seekg(0, std::ios::beg);
fl.read(ret, len);
fl.close();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
注意I:我需要找到一种方法来确保可以从文件中恢复位"00000000"!
注意二:有关将这些位"00000000"保存到文件的安全方法的建议吗?
注3:当使用char数组时,我在转换该类型的位"00000000"时遇到了问题.
代码片段:
int bit8Array[] = {0, 0, 0, 0, 0, 0, 0, 0};
char charByte = (bit8Array[7] ) |
(bit8Array[6] << 1) |
(bit8Array[5] << 2) |
(bit8Array[4] << 3) |
(bit8Array[3] << 4) |
(bit8Array[2] << 5) |
(bit8Array[1] …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Redis (redis.conf,bind参数)配置为仅接受来自某些 ip 的访问。就我而言,我想启用对环回网络接口 ( 127.0.0.1/ ::1) 和 ip 192.168.56.101(192.168.56.102是 Redis 服务器的 ip)的访问。根据我到目前为止阅读的所有文档,下面的配置应该有效......
bind 127.0.0.1 ::1 192.168.56.101
Run Code Online (Sandbox Code Playgroud)
...但事实并非如此。
我尝试过其他几种配置...
bind 127.0.0.1 192.168.56.101 ::1
bind 127.0.0.1 192.168.56.101
bind 192.168.56.101
bind 192.168.56.0
bind 192.168.0.0
Run Code Online (Sandbox Code Playgroud)
...但没有任何效果。=|
唯一有效的配置是......
bind 0.0.0.0
Run Code Online (Sandbox Code Playgroud)
但是,此配置可以访问任何 IP!
注意:参数protected-mode( redis.conf) 有一个no值。
知道会发生什么吗?
参考:
进一步的问题:
如何启用对 IP 范围(bind参数)的访问?就像是...
bind 192.168.56.0
Run Code Online (Sandbox Code Playgroud)
... 或者...
bind 192.168.56.0/24
Run Code Online (Sandbox Code Playgroud)
在这些示例中,任何 IP 以“192.168.56”开头的计算机都可以访问 Redis 服务器。
@卡尔达科斯塔@Jacky …
介绍:
它是一个使用Python 3.2、NGINX和uWSGI的Web应用程序环境。
Python 3.2、NGINX 和 uWSGI 组件是通过构建和安装(“./configure”、“make”和“make install”)安装的,它们使用的 OpenSSL 1.0.X 也通过构建和安装安装。
该应用程序在 Ubuntu 20.04 LTS 上运行。
问题:
应用程序无法启动,uWSGI 日志显示以下信息...
Thu Jan 21 21:44:37 2021 - Respawned uWSGI worker 4 (new pid: 99572)
Thu Jan 21 21:44:37 2021 - worker 3 buried after 0 seconds
Thu Jan 21 21:44:37 2021 - worker 7 buried after 0 seconds
Thu Jan 21 21:44:37 2021 - worker 1 buried after 0 seconds
Thu Jan 21 21:44:37 2021 - worker 8 buried after 0 …Run Code Online (Sandbox Code Playgroud) 我使用“writeFileBytes”下面的函数std::vector<unsigned char>将 a的内容写入文件。我想使用“无符号字符”类型来保存文件(请注意,这是为“字符”进行的强制转换)。这个问题的原因是因为“无符号字符”与任何字节(例如“00000000”位)兼容。当我们使用“char”时,我们在操作某些“无效”字符时会遇到一些问题。
请参阅此主题最适合保留文件字节的向量类型是什么?有关“00000000”位(1 字节)问题的更多信息。
void writeFileBytes(const char* filename, std::vector<unsigned char>& fileBytes){
std::ofstream file(filename, std::ios::out|std::ios::binary);
file.write(fileBytes.size() ? (char*)&fileBytes[0] : 0,
std::streamsize(fileBytes.size()));
}
writeFileBytes("xz.bin", fileBytesOutput);
Run Code Online (Sandbox Code Playgroud)
有没有办法在本地使用“无符号字符”类型来写入文件?
这种担心真的有意义吗?
更新一:
有没有办法在本地使用“无符号字符”类型来写入文件?->是的!
遵循 krzaq 的指导方针!
void writeFileBytes(const char* filename, std::vector<unsigned char>& fileBytes){
std::ofstream file(filename, std::ios::out|std::ios::binary);
std::copy(fileBytes.cbegin(), fileBytes.cend(),
std::ostream_iterator<unsigned char>(file));
}
Run Code Online (Sandbox Code Playgroud)
更新二:
这种担心真的有意义吗?->在某些方面,是的!
正如我在下面评论...
“...'unsigned char' 似乎具有'更高级别的兼容性'(包括 '00000000' 位)。当我们尝试将这 8 位('00000000' 位)转换为 'char' 时,我们没有与 ' 不同的值unsigned char'。使用 'unsigned char' 我们有一个无效/无法识别的 'char' 值,但我们有......”
请参阅此主题最适合保留文件字节的向量类型是什么?想要查询更多的信息!