如何以二进制形式打开一个任意格式文件,然后每次读出若干字节呢?

galaxy999 2003-10-19 02:04:30
比如打开一个文件,每次读一个字节,依次放入一个数组。
以下是我写的代码:
unsigned char bitchar[16];
char * bitchar1;
ifstream myfile( "文件名和路径",ios::in|ios::binary);
for(int i=0;i<16;i++) // 假设总共只读16个字节
{
myfile.get((char*)bitchar1,1); //每次读一个放到数组中
bitchar[i]=*bitchar1;
}

for(int i=0;i<16;i++)
{
cout<<"bitchar[i]<<endl;// 把数组中内容依次显示。
}
在myfile.get((char*)bitchar1,1)这句中如果第二个参数用1的话,读不出来内容,
如果改成myfile.get((char*)bitchar1,2);反而能每次读出一个字节。
请大家分析一下这是什么原因呢?

另外,还有没有更好的方法一次读出若干字节呢?比如每次读8个,知道把整个文件读完。

...全文
192 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
galaxy999 2003-10-22
  • 打赏
  • 举报
回复
难道是我编译器出问题了?或者是机器的问题?
把你调好的程序整个贴给我看看把!
0sch 2003-10-21
  • 打赏
  • 举报
回复
你的程序没有问题!!!!!!!

我把你的程序拷下来调了一下,完全正确。

是不是你所读的文件本来就不对????

我是这么测试的:

void main()
{
ofstream myfile( "1.txt",ios::out|ios::binary);
char a='b';
for(int i=0;i<16;i++)
myfile<<a;
myfile.close();
fn();

}

在fn()中将文件改为"1.txt",我的机子上没有那个文件
galaxy999 2003-10-20
  • 打赏
  • 举报
回复
myfile.get(&bitchar1,1);
bitchar[i]=*bitchar1;

这样还是不行得
celxta 2003-10-20
  • 打赏
  • 举报
回复
语法错误:
改为:

unsigned char bitchar[16];
char bitchar1; --------------------修改
ifstream myfile( "文件名和路径",ios::in|ios::binary);
for(int i=0;i<16;i++) // 假设总共只读16个字节
{
myfile.get(&bitchar1,1); //每次读一个放到数组中 --------------------修改

bitchar[i]=*bitchar1;
}

for(int i=0;i<16;i++)
{
cout<<"bitchar[i]<<endl;// 把数组中内容依次显示。
}

zhang_zhibin 2003-10-19
  • 打赏
  • 举报
回复
FILE* fp = fopen( "filename", "rb" );
if ( fp )
{
fread...
}
galaxy999 2003-10-19
  • 打赏
  • 举报
回复
哦,为什么bitchar要设置为9个元素的呢?
我把全部程序贴出来好了,
#include <fstream>
#include <iostream>
using namespace std;
void fn()
{

ifstream myfile( "D:\\C-study\\DES-project\\words2.txt",ios::in|ios::binary);
if(myfile.fail())
{
cout<<"The file does not exist";
system("PAUSE");
}
else
{
unsigned char bitchar[16];
char bitchar1;
int bitarray8[8];
int bitarray64[64];
unsigned char chartemp;

for(int t=0;t<16;t++)
{
//myfile.get((char*)&bitchar[t],2);//读出了文件中的2位,但是bitchar中只存了1个,为什么呢?
myfile.get(&bitchar1,2); //用这个语句还是得读2位。
bitchar[t]=bitchar1;
}

for(int i=0;i<16;i++)
{
cout<<"The "<<(i)<<" bit: "<<bitchar[i]<<" "<<endl;
}

cout<<endl;


cout<<"以下是8位字符的比特位表示:"<<endl;

for( t=0; t<8; t++)
{
chartemp=bitchar[t];
for(i=7;i>=0;i--)
{
bitarray8[i]=(((chartemp>>(7-i)))&1);
}
for( i=0; i<8;i++)
{
bitarray64[t*8+i]= bitarray8[i];
}
}

for( t=0;t<8;t++)
{
cout<<"The "<<(t+1)<<" Bety: " ;
for(i=0;i<8;i++)
{
cout<<bitarray64[t*8+i];
}
cout<<"---"<<bitchar[t]<<endl;
}

system("PAUSE");
}


}

void main()
{
fn();

}
0sch 2003-10-19
  • 打赏
  • 举报
回复
char* bitcar1=new char[9];
.
.
.
while(!myfile.eof())
{
myfile.get(bitchar1,8*sizeof(char));
for(int i=0;i<8;i++)
bitchar[i]=bitchar1[i];
}
.
.
.
delete bitcar1;

//字符数组的末尾可能是'\0',根据系统不同,可能不同。

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧