文件读写文本文件和二进制文件的区别

jeckma 2006-01-21 06:00:05
int main(int argc, char* argv[])
{
int a =1234;

FILE * file;
file = fopen("test.log","wr");

fwrite(&a,sizeof(char),1,file);
fclose(file);

FILE * file1;
file1 = fopen("test1.log","wrb");

fwrite(&a,sizeof(char),1,file1);
fclose(file1);
return 0;
}

打开生成的文件可以看到没有任何区别。那它们的区别在什么地方呢?
...全文
554 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
why25 2006-01-22
  • 打赏
  • 举报
回复
学习~~~~
jeckma 2006-01-22
  • 打赏
  • 举报
回复
megaboy(飞天御剑流之杀神一刀斩)的解释真不错,以前一直以为区别在数字上,现在才明白是在特殊字符上。另外你那个C89/C99标准有下载地址吗,很想看看。
Tiwen 2006-01-21
  • 打赏
  • 举报
回复
ASCII码文件可在屏幕上按字符显示, 例如源程序文件就是ASCII文件,用DOS命令TYPE可显示文件的内容。 由于是按字符显示,因此能读懂文件内容。

  二进制文件是按二进制的编码方式来存放文件的。 例如, 数5678的存储形式为: 00010110 00101110只占二个字节。二进制文件虽然也可在屏幕上显示, 但其内容无法读懂。C系统在处理这些文件时,并不区分类型,都看成是字符流,按字节进行处理。 输入输出字符流的开始和结束只由程序控制而不受物理符号(如回车符)的控制。 因此也把这种文件称作“流式文件”。
watermelontx 2006-01-21
  • 打赏
  • 举报
回复
找一个二进制文件测试一下...
kikikind 2006-01-21
  • 打赏
  • 举报
回复
你在传文件时~~~只能以二进制方式打开exe的文件进行~~~~
megaboy 2006-01-21
  • 打赏
  • 举报
回复
二进制与文本方式的区别不是这样看的,两者的主要区别,是文本方式允许在输入或输出时,对某些字符进行替换、增加或删除等操作。在windows下,主要的区别就是写入时\n会被替换为\r\n,输出时\r\n又会被重新组合为\n。请看C89/C99关于两种方式的规定:

A text stream is an ordered sequence of characters composed into lines, each line
consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one to-one correspondence between the characters in a stream and those in the external representation.

A binary stream is an ordered sequence of characters that can transparently record
internal data. Data read in from a binary stream shall compare equal to the data that were earlier written out to that stream, under the same implementation. Such a stream may, however, hav e an implementation-defined number of null characters appended to the end of the stream.
jeckma 2006-01-21
  • 打赏
  • 举报
回复
就是看了没有区别才问啊?
why25 2006-01-21
  • 打赏
  • 举报
回复
一个记事本文件你用两中方法读读看就知道那是有区别地~~

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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