
C++有关文件的问题
基本要求:编写程序,用二进制方式读写指定的文件a.bin,在每一行前加行号,写入另一个二进制文件b.bin中,从b.bin中读取数据输出到屏幕。基本步骤:编写程序lab6...
基本要求:编写程序,用二进制方式读写指定的文件a.bin,在每一行前加行号,写入另一个二进制文件b.bin中,从b.bin中读取数据输出到屏幕。
基本步骤:编写程序lab6_1.cpp,使用void main(int argc, char*argv[])函数中的参数传递操作的文件名,定义ofstream的对象对文件进行操作,使用read()和write()成员函数读入数据,使用输出流对象输出数据到文件和屏幕。
请各位高手帮个忙吧
写出代码就行 展开
基本步骤:编写程序lab6_1.cpp,使用void main(int argc, char*argv[])函数中的参数传递操作的文件名,定义ofstream的对象对文件进行操作,使用read()和write()成员函数读入数据,使用输出流对象输出数据到文件和屏幕。
请各位高手帮个忙吧
写出代码就行 展开
2个回答
展开全部
分确实有点少啊。不过近段时间重拾C++,就当复习了。调试的时候有点艰辛,一度想要放弃,不过还是成功了,也学到了以前从未学到的东西,纠正了原来一些错误认识,呵呵。题目描述有点混乱,让人迷惑的是为什么要把二进制文件输出到屏幕,如果以字符形式输出到屏幕那就是想看乱码和听嘟嘟声了,这里以十六进制输出。二进制文件怎么才算一行?姑且还是以0A(H)分隔吧。还要加上行号,如果读的是文本文件且里面只有英文字符,写出来的文件用记事本打开还可以看得出效果,否则就一堆乱码。看看吧,不知道是否合你意。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void main(int argc,char *argv[])
{
if(argc!=3)
{
cout<<"分别输入要读、写的文件名!\n";
return;
}
int count=1;
int rowCount=0;
ifstream infs;
ofstream outfs;
ios::pos_type pt;
bool lastLine=false;
infs.open(argv[1],ios::in|ios::binary);
outfs.open(argv[2],ios::out|ios::binary|ios::trunc);
if(!infs||!outfs)
{
cout<<"打开文件失败!\n";
return;
}
while(!infs.eof())
{
pt=infs.tellg();//记录初始或某一行起始位置
while (infs.get()!=10&&!infs.eof())
{
count++;
}
if(infs.eof())
{
count--;
infs.clear();
lastLine=true;
}
infs.seekg(pt);//回到本行起始位置
char *readBuf;
readBuf=new char[count];
infs.read(readBuf,count);//读取本行
char *writeBuf;
writeBuf=new char[count+1];
*writeBuf=(char)(++(rowCount)+48);//添加行号
for(int i=1;i<=count;i++)
{
writeBuf[i]=readBuf[i-1];
}
if(count>0)
outfs.write(writeBuf,count+1);
delete []writeBuf;
delete []readBuf;
if(lastLine)
break;
count=1;
}
infs.close();
outfs.close();
infs.open(argv[2],ios::in|ios::binary);
char oneByte;
infs.get(oneByte);
while(!infs.eof())
{
if(oneByte==10)
cout<<oneByte;
else
cout<<setw(2)<<setfill('0')<<setiosflags (ios::uppercase)<<hex<<(int)(oneByte&255)<<' ';
infs.get(oneByte);
}
infs.close();
cout<<endl;
return;
}
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void main(int argc,char *argv[])
{
if(argc!=3)
{
cout<<"分别输入要读、写的文件名!\n";
return;
}
int count=1;
int rowCount=0;
ifstream infs;
ofstream outfs;
ios::pos_type pt;
bool lastLine=false;
infs.open(argv[1],ios::in|ios::binary);
outfs.open(argv[2],ios::out|ios::binary|ios::trunc);
if(!infs||!outfs)
{
cout<<"打开文件失败!\n";
return;
}
while(!infs.eof())
{
pt=infs.tellg();//记录初始或某一行起始位置
while (infs.get()!=10&&!infs.eof())
{
count++;
}
if(infs.eof())
{
count--;
infs.clear();
lastLine=true;
}
infs.seekg(pt);//回到本行起始位置
char *readBuf;
readBuf=new char[count];
infs.read(readBuf,count);//读取本行
char *writeBuf;
writeBuf=new char[count+1];
*writeBuf=(char)(++(rowCount)+48);//添加行号
for(int i=1;i<=count;i++)
{
writeBuf[i]=readBuf[i-1];
}
if(count>0)
outfs.write(writeBuf,count+1);
delete []writeBuf;
delete []readBuf;
if(lastLine)
break;
count=1;
}
infs.close();
outfs.close();
infs.open(argv[2],ios::in|ios::binary);
char oneByte;
infs.get(oneByte);
while(!infs.eof())
{
if(oneByte==10)
cout<<oneByte;
else
cout<<setw(2)<<setfill('0')<<setiosflags (ios::uppercase)<<hex<<(int)(oneByte&255)<<' ';
infs.get(oneByte);
}
infs.close();
cout<<endl;
return;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询