输出文件中的内容

swh326 2009-07-26 02:44:18

# include <iostream>
# include <fstream>
using namespace std;
int main()
{
ifstream infile("c:\\a.cap",ios::binary);
while(!infile.eof())
cout.write<<infile;
return 0;
}

我也想把这个文件c:\\a.cap里的数据按它存储的方式输出到屏幕上(就是输出0110那种形式)
不知道怎么做
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2009-07-26
  • 打赏
  • 举报
回复
7?
7=8-1
char在计算机中占8bit ^_^
mstlq 2009-07-26
  • 打赏
  • 举报
回复
只有跟流相关的<<和>>有读入读出的功能。
<<和>>更一般的功能是位移。

建议楼主了解一下“位操作”相关的概念。
请参阅帖子^_^
http://topic.csdn.net/t/20041019/00/3468250.html
swh326 2009-07-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mstlq 的回复:]
总共几个0呢?
很多吗?如果是的话,那我怀疑一下,你的测试机子上有没有c:\a.cap这个文件呢?^_^

加个条件测试看看^_^
C/C++ code# include<iostream>
# include<fstream>usingnamespace std;void inline showBinary(unsignedchar data)
{for(int i=7;i>=0;i--)if(data& (1<<i))
cout<<'1';else
cout<<'0';
};int main()
{
ifstream infile("c:\\a.cap",ios::binary);char c;if(!infile.is_open()) {cout<<"file not found!"<<endl;}else{while(!infile.eof()){
infile.read(&c,1);
showBinary(c);
}
}
system("pause");return0;
}
[/Quote]
不好意思 ~~果然向你说的那样 我把后缀去掉就好了~~
我想再问一下~~
for(int i=7;i>=0;i--)if(data& (1<<i))什么意思 data应该是有数据读入的意思吧
那1<<i呢?还有那个7是怎么来的?谢谢前辈O(∩_∩)O~~
mstlq 2009-07-26
  • 打赏
  • 举报
回复
总共几个0呢?
很多吗?如果是的话,那我怀疑一下,你的测试机子上有没有c:\a.cap这个文件呢?^_^

加个条件测试看看^_^
# include <iostream>
# include <fstream>

using namespace std;

void inline showBinary(unsigned char data)
{
for(int i=7;i>=0;i--)
if(data & (1<<i))
cout<<'1';
else
cout<<'0';
};


int main()
{
ifstream infile("c:\\a.cap",ios::binary);
char c;
if(!infile.is_open()) {cout<<"file not found!"<<endl;}
else{
while(!infile.eof()){
infile.read(&c,1);
showBinary(c);
}
}
system("pause");
return 0;
}
swh326 2009-07-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mstlq 的回复:]
刚才代码没贴好,>_ <C/C++ code# include<iostream>
# include<fstream>usingnamespace std;void inline showBinary(unsignedchar data)
{for(int i=7;i>=0;i--)if(data& (1<<i))
cout<<'1';else
cout<<'¡­
[/Quote]
全部显示的是0 dev c++

我的意思就是按数据在内存中存储的样子输出
mstlq 2009-07-26
  • 打赏
  • 举报
回复
刚才代码没贴好,>_<
# include <iostream>
# include <fstream>

using namespace std;

void inline showBinary(unsigned char data)
{
for(int i=7;i>=0;i--)
if(data & (1<<i))
cout<<'1';
else
cout<<'0';
};


int main()
{
ifstream infile("c:\\a.cap",ios::binary);
char c;
while(!infile.eof()){
infile.read(&c,1);
showBinary(c);
}
system("pause");
return 0;
}
mstlq 2009-07-26
  • 打赏
  • 举报
回复
要看0101?
随手写了一个,手头没有编译器,未经测试^_^

[code=C/C++]
# include <iostream>
# include <fstream>

using namespace std;

void inline showBinary(unsigned char data)
{
for(int i=7;i>=0;i--)
if(data & (1<<i))
cout<<'1';
else
cout<<'0';
};


int main()
{
ifstream infile("c:\\a.cap",ios::binary);
char c;
while(!infile.eof()){
infile.read(&c,1);
showBinary(c);
}
system("pause");
return 0;
}
code]

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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