因为文件一事差点怀疑编译器,三份代码对比
来源:互联网 发布:多益网络面试 策划 编辑:程序博客网 时间:2024/06/05 17:52
//因为这里不小心把文件关了?!!!ifs.close();#include<cstdio>#include<fstream>#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;ifs.seekg(0,ios::end);cout<<"The whole chars of this file is "<<ifs.tellg()<<endl;//因为这里不小心把文件关了?!!!ifs.close();len=ifs.tellg()/4;//find len firstly dont use !ifs ,because it may cause more EOF ifs.seekg(0,ios::beg);while(count<len){//!ifs means what ifs.read(reinterpret_cast<char*>(&txt[count]),sizeof(int));//将读取到的数强制转换成char型,因为这个txt是char型指针 count++;}cout<<count<<endl;ifs.close();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;}
DeadL7.cpp
#include<cstdio> #include<fstream> #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; }
DeadAgainL7.cpp
#include<cstdio> #include<fstream> #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型指针 count++; } cout<<count<<endl; ifs.close(); 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
- 因为文件一事差点怀疑编译器,三份代码对比
- 因为编译器顺序读取文件
- 编译器级别的代码优化对比
- 编译器级别的代码优化对比
- 写代码一定要敢于怀疑
- 怀疑
- “差点”
- 三份查找
- 差点被一段HTML代码害死
- ACM-ACMICPC (AC,动态规划/遍历所有字串,三份代码)
- 编译器三:GCC,LLVM,Clang编译器对比
- 租房一事
- 每天一事
- 每天一事
- openstack一事
- 语法文件解释器及编译器代码生成
- 敲代码敲到怀疑人生,开发者们该怎么办?
- GCC,LLVM,Clang编译器对比
- Git 常用命令及场景
- 对组件的引用(refs)
- java中枚举的使用
- AOP面向切面编程简介
- Python爬虫初识
- 因为文件一事差点怀疑编译器,三份代码对比
- OC正则表达式的简单使用
- 【R语言或Python语言入门必读】大数据分析师到底需要做什么(一)
- Java注解全解析(附)——javapoet简介
- 自定义color文件夹来改变字体颜色
- java简单排序
- 正则表达式
- Redis Cluster 3.0搭建与使用
- CSS学习之二实践盒子