fread 读取到std::string里
时间: 2023-08-01 20:11:36 浏览: 116
可以使用`std::ifstream`读取文件,然后将数据读取到一个`std::stringstream`中,最后将`std::stringstream`中的内容赋值给一个`std::string`。示例代码如下:
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main() {
std::ifstream ifs("test.txt");
if (!ifs.is_open()) {
std::cerr << "Failed to open file!" << std::endl;
return -1;
}
std::stringstream ss;
ss << ifs.rdbuf();
std::string content = ss.str();
std::cout << content << std::endl;
ifs.close();
return 0;
}
```
其中,`test.txt`是要读取的文件名,`content`是读取到的文件内容。
相关问题
linux std::string 中文乱码怎么解决
在 Linux 中,`std::string` 类型是以 UTF-8 编码存储字符串的。如果你的程序中出现了中文乱码,有可能是以下原因:
1. 程序读入字符串时没有使用正确的字符编码;
2. 程序输出字符串时没有设置正确的字符编码;
3. 程序中使用了不支持 UTF-8 编码的库函数。
针对第一种情况,需要检查程序中读入字符串的代码,确保使用的字符编码是 UTF-8。如果你是通过文件读取字符串,可以使用 `iconv` 函数将其转换为 UTF-8 编码。例如:
```c++
#include <iconv.h>
#include <string>
std::string read_file(const char* filename, const char* encoding) {
FILE* fp = fopen(filename, "r");
if (!fp) {
return "";
}
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buf = new char[size + 1];
size_t nread = fread(buf, 1, size, fp);
buf[nread] = '\0';
fclose(fp);
std::string result;
iconv_t cd = iconv_open("UTF-8", encoding);
if (cd != (iconv_t)(-1)) {
size_t inbytesleft = size;
size_t outbytesleft = size * 4; // 为转换后的字符串分配足够的内存
char* inbuf = buf;
char* outbuf = new char[outbytesleft];
char* outptr = outbuf;
if (iconv(cd, &inbuf, &inbytesleft, &outptr, &outbytesleft) != (size_t)(-1)) {
result = std::string(outbuf, outptr - outbuf);
}
delete[] outbuf;
iconv_close(cd);
}
delete[] buf;
return result;
}
```
针对第二种情况,需要检查程序输出字符串的代码,确保使用的字符编码是 UTF-8。如果你是通过终端输出字符串,可以使用 `export LANG=en_US.UTF-8` 命令设置终端的字符编码为 UTF-8。
针对第三种情况,需要检查程序中使用的库函数是否支持 UTF-8 编码。如果不支持,可以考虑使用支持 UTF-8 编码的库函数替代。例如,使用 `libiconv` 库进行编码转换。
libyuv::YUY2ToNV12的使用demo
以下是使用 libyuv::YUY2ToNV12 进行图像转换的示例代码:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "libyuv.h"
int main(int argc, char* argv[]) {
const char* src_filename = "input.yuy2";
const char* dst_filename = "output.nv12";
const int src_width = 640;
const int src_height = 480;
// 打开输入文件
FILE* src_file = fopen(src_filename, "rb");
if (!src_file) {
std::cerr << "Could not open source file: " << src_filename << std::endl;
return -1;
}
// 打开输出文件
FILE* dst_file = fopen(dst_filename, "wb");
if (!dst_file) {
std::cerr << "Could not open destination file: " << dst_filename << std::endl;
return -1;
}
// 准备输入帧
uint8_t* src_frame = (uint8_t*)malloc(src_width * src_height * 2);
// 准备输出帧
uint8_t* dst_y = (uint8_t*)malloc(src_width * src_height);
uint8_t* dst_uv = (uint8_t*)malloc(src_width * src_height / 2);
// 读取输入帧
if (fread(src_frame, 1, src_width * src_height * 2, src_file) != src_width * src_height * 2) {
std::cerr << "Could not read input frame" << std::endl;
return -1;
}
// 进行图像转换
libyuv::YUY2ToNV12(src_frame, src_width * 2, dst_y, src_width, dst_uv, src_width, src_width, src_height);
// 写入输出帧
if (fwrite(dst_y, 1, src_width * src_height, dst_file) != src_width * src_height) {
std::cerr << "Could not write output frame" << std::endl;
return -1;
}
if (fwrite(dst_uv, 1, src_width * src_height / 2, dst_file) != src_width * src_height / 2) {
std::cerr << "Could not write output frame" << std::endl;
return -1;
}
// 释放资源
fclose(src_file);
fclose(dst_file);
free(src_frame);
free(dst_y);
free(dst_uv);
return 0;
}
```
代码中的 YUY2ToNV12 图像转换使用了 libyuv 库进行实现。在代码中,我们首先打开输入文件和输出文件,并准备输入帧和输出帧的内存空间。然后,我们读取输入帧,进行图像转换,最后将输出帧写入输出文件。最后,我们释放资源并返回。注意,由于 NV12 格式中的 UV 分量是交错存储的,因此在写入输出文件时需要分别写入 Y 分量和交错的 UV 分量。