写一段bin转txt格式代码
时间: 2023-03-24 13:02:26 浏览: 685
以下是一段将二进制文件转换为文本文件的代码:
```c
#include <stdio.h>
int main() {
FILE *bin_file = fopen("input.bin", "rb");
FILE *txt_file = fopen("output.txt", "w");
if (bin_file == NULL || txt_file == NULL) {
printf("Failed to open file.\n");
return 1;
}
int c;
while ((c = fgetc(bin_file)) != EOF) {
fprintf(txt_file, "%02X ", c);
}
fclose(bin_file);
fclose(txt_file);
return ;
}
```
这段代码将打开名为 "input.bin" 的二进制文件,并将其转换为名为 "output.txt" 的文本文件。在转换过程中,每个字节都被转换为两个十六进制数字,并用空格分隔。
相关问题
写一段c++的bin转txt的代码
以下是将二进制文件转换为文本文件的C代码:
```c
#include <stdio.h>
int main() {
FILE *bin_file = fopen("binary_file.bin", "rb");
FILE *txt_file = fopen("text_file.txt", "w");
if (bin_file == NULL || txt_file == NULL) {
printf("Failed to open file.\n");
return 1;
}
int byte;
while ((byte = fgetc(bin_file)) != EOF) {
fprintf(txt_file, "%02X ", byte);
}
fclose(bin_file);
fclose(txt_file);
return 0;
}
```
这段代码打开一个名为"binary_file.bin"的二进制文件,并将其转换为文本文件"test_file.txt"。在循环中,它读取每个字节并将其格式化为两个十六进制数字,然后写入文本文件中。最后,关闭两个文件并返回0表示成功。
写一段txt点云转换成bin格式的代码
以下是将txt点云转换成bin格式的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Point {
float x;
float y;
float z;
};
int main() {
string input_file = "input.txt";
string output_file = "output.bin";
vector<Point> points;
// 读取txt文件中的点云数据
ifstream fin(input_file);
if (!fin) {
cerr << "无法打开文件:" << input_file << endl;
return -1;
}
float x, y, z;
while (fin >> x >> y >> z) {
Point p = {x, y, z};
points.push_back(p);
}
fin.close();
// 将点云数据写入bin文件
ofstream fout(output_file, ios::binary);
if (!fout) {
cerr << "无法创建文件:" << output_file << endl;
return -1;
}
for (const auto& p : points) {
fout.write(reinterpret_cast<const char*>(&p), sizeof(Point));
}
fout.close();
cout << "点云数据已成功转换为bin格式!" << endl;
return ;
}
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)