文件的转置

Problem L7_F3_Z1
Write a program that reverses the order of integers stored in a binary file L7_F3_Z1.dat. Do not use
additional files.
#include<cstdio>
#include<fstream>
#include<iomanip>
#include<iostream>
using namespace std;
//Ö®ËůŇÔgcountĘäłö10Ćäʵ˛»ĘÇ10Ł¬ĆäʵĘÇ16.ŇňÎŞµ±ÓĂŐâ¸ölen×÷forŃ­»·µÄʱşňĆäʵĘÇ16 
int main(){
	ifstream ifs("L8_F3_Z1.dat",ios::binary);
	if(!ifs){
		cout<<"Error opening file\n";
		return 1; 
	} 
	int len;
	unsigned char txt[100][4];
	int number[1000];
	int count=0;
	cout<<sizeof(int)<<endl;
	string sentence; 

	ifs.seekg(0,ios::end);
	cout<<"The whole chars of this file is "<<ifs.tellg()<<endl;
	//×îşóÍęłÉÖ»ĘǸÄÁËŐâŔď¶řŇŃŁ¬ĆäʵҲûÓĐ×öʲôŁ¬żÉÄÜľÍĘÇÓď·¨µÄÉčÖĂÎĘĚâ°É 
	len=ifs.tellg()/4;
	ifs.seekg(0,ios::beg);
	while(count<len){//!ifs means what 
		ifs.read(reinterpret_cast<char*>(&txt[count]),sizeof(int));//˝«¶Áȡµ˝µÄĘýÇżÖĆת»»łÉcharĐÍŁ¬ŇňÎŞŐâ¸ötxtĘÇcharĐÍÖ¸Őë 
		
		cout<<"txt:"<<txt[count]<<endl;
		cout<<"txt1:"<<setfill('0')<<setw(2)<<hex<<static_cast<int>(txt[count][1]-'0')<<":";
		cout<<"txt2:"<<setfill('0')<<setw(2)<<hex<<static_cast<int>(txt[count][2]-'0')<<":";
		cout<<"txt3:"<<setfill('0')<<setw(2)<<hex<<static_cast<int>(txt[count][3]-'0')<<":";
		cout<<"txt4:"<<setfill('0')<<setw(2)<<hex<<static_cast<int>(txt[count][4]-'0')<<endl;
		
//		int txt1=(txt[1]-'0')<<12;
//		int txt2=(txt[2]-'0')<<8;
//		int txt3=(txt[3]-'0')<<4;
//		int txt4=txt[4]-'0';
//		number[count]=txt1+txt2+txt3+txt4;
//		cout<<number[count]<<endl;
		count++;
	}
	cout<<count<<endl;
	ifs.close();
	
	//before open it, clear all 
	ofstream ofs("L8_F3_Z1.dat",ios::binary|ios::out|ios::trunc);
	
	if(!ofs){
		cout<<"Error opening file\n";
		return 1; 
	} 
	ofs.seekp(0,ios::end);
	int nowPosition=ofs.tellp();
	cout<<"The position now is "<<nowPosition<<endl;
	
	ofs.seekp(0,ios::beg);
	for(int i=0;i<count;i++){
		ofs.seekp(i*4,ios::beg);
		ofs.write(reinterpret_cast<char*>(&txt[count-i-1]), sizeof(int));
	}
	ofs.close();
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值