关于文件指针的使用

yellowken2001 2008-09-01 11:48:57
#include <time.h>
#include <iomanip.h>
#include <stdio.h>
int main()
{

int c,i;
int a[16];
FILE *file;
if((file = fopen("1.data", "rb"))==NULL)
{
printf("can not open\n");
}
else
{
printf("file open succeed\n");
}
for (i=1;i<16;i++)
{
fread(&a[i],sizeof(int),1,file);
printf("µØÖ·Îª%d\n",a[i]);
printf("µÄÊý¾ÝΪ%x\n",&a[i]);
}
fclose(file);
return 1;
}
上面是我的程序,我的本意将文件中的数据一个一个读出并打印为0X00这种格式,就是读一个,如原始文件为0x00,0x01,0x02,读出来后,打印出来就是
地址为***的数据为0x**,但是小弟半年多没写程序了,一时忘记用法了,各位帮帮忙,分散就是
...全文
168 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiyijiji 2008-09-01
  • 打赏
  • 举报
回复
#include <time.h>
#include <iomanip.h>
#include <stdio.h>
int main()
{

int c,i;
int a[16];
FILE *file;
if((file = fopen("1.data", "rb"))==NULL)
{
printf("can not open\n");
exit(1);
}
else
{
printf("file open succeed\n");
}
for (i=1;i <16;i++)
{
int c;
fread(&c,sizeof(int),1,file);
printf("0x%02x\n",c);
fclose(file);
return 1;
}
冷月清晖 2008-09-01
  • 打赏
  • 举报
回复
%#x
jieao111 2008-09-01
  • 打赏
  • 举报
回复
if((file = fopen("1.data", "rb"))==NULL)
{
printf("can not open\n");
return 0;
}
  • 打赏
  • 举报
回复
#include <time.h>
#include <iomanip.h>
#include <stdio.h>
int main()
{

int c,i;
int a[16];
FILE *file;
if((file = fopen("1.data", "rb"))==NULL)
{
printf("can not open\n");
}
else
{
printf("file open succeed\n");
}
for (i=1;i <16;i++)
{
char c;
fread(&c,sizeof(char),1,file);
printf("0x%02x\n",c);
fclose(file);
return 1;
}
yellowken2001 2008-09-01
  • 打赏
  • 举报
回复
谢谢各位,特别感谢调试的朋友
K行天下 2008-09-01
  • 打赏
  • 举报
回复
你写成return 0也行啊
Wolf_FienD 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 akirya 的回复:]
#include <time.h>
#include <iomanip.h>
#include <stdio.h>
int main()
{

int c,i;
int a[16];
FILE *file;
if((file = fopen("1.data", "rb"))==NULL)
{
printf("can not open\n");
}
else
{
printf("file open succeed\n");
}
for (i=1;i <16;i++)
{
char c;
fread(&c,sizeof(char),1,file);
printf("0x%02x\n",c);
fclose(file);
return 1;
}
[/Quote]


为何 return 1 呢?

不是 return 0 的吗?
太乙 2008-09-01
  • 打赏
  • 举报
回复
up~~解决了?
星羽 2008-09-01
  • 打赏
  • 举报
回复

nclude <stdio.h>

int main()
{
FILE* file = fopen("1.data", "rb");
unsigned char ch = 0;
int cnt = 0;

if (!file)
{
printf("can not open the file\n");
return 1;
}

printf("open file success\n\n");

while (1)
{
fread(&ch, sizeof(ch) , 1, file);
printf("%s0x%02x ", cnt++ % 10 ? "" : "\n", ch);
if (feof(file))
break;
}

fclose(file);
return 0;

}

// test

open file success


0x61 0x64 0x66 0x61 0x73 0x66 0x61 0x73 0x64 0x66
0x61 0x73 0x66 0x61 0x73 0x64 0x66 0x61 0x73 0x64
0x66 0x61 0x73 0x64 0x66 0x61 0x73 0x66 0x66 请按任意键继续. . .








65,208

社区成员

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

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