C++环境下如何进行io流读取txt文件,通过string进行接收,并且转化为10进制的数据进行存储。

代码如下:

#include<fstream>
#include<iostream>
#include<string>
#include <stdio.h>
#include <cstdlib>

using namespace std;
int main()
{
	string string1;
	int result;
	ifstream infile("lidar_data_demo.txt",ios::in);

	if(!infile)
	{
		cout<<"open error!"<<endl;
		exit(1);
	}
	ofstream outfile("输出2.txt");
	for(int i=0;i<541;i++)
	{
		infile>>string1;
		//十六进制转十进制 
		char* end;
	 	result = static_cast<int>(strtol(string1.c_str(),&end,16)); 
		outfile<<result<<" ";	
	}
	cout<<endl;

	infile.close();
	outfile.close();
	return 0;	
} 

其中lidar_data_demo.txt文件为十六进制文件,如下:

C++中进行TXT文件的读入、转化和写入

转化后如下:

C++中进行TXT文件的读入、转化和写入

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-17
  • 2021-10-05
  • 2023-01-25
  • 2022-02-24
相关资源
相似解决方案