fopen 中 按文本读写与按二进制读写 实例
生活随笔
收集整理的這篇文章主要介紹了
fopen 中 按文本读写与按二进制读写 实例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考:http://blog.csdn.net/hinyunsin/article/details/6401854
#include <stdio.h>int main(int argc, char *argv[])
{char he[20] = "hello world\n";FILE *outfile = fopen("t.txt", "wt");fwrite(he, sizeof(char), 20, outfile);fclose(outfile);outfile = fopen("b.txt", "wb");fwrite(he, sizeof(char), 20, outfile);fclose(outfile);return 0;
}用WinHex查看本地的兩個(gè)文件
t.txt
b.txt
可以看到按文本寫(xiě)時(shí) fopen 把 '\n' 替換為了 0D0A
對(duì)應(yīng)的,讀文件時(shí)
#include <stdio.h>
#include <string.h>void echo(char *sz)
{int i = 0;while(sz[i]){printf("%x ", sz[i]);++i;}printf("\n");
}int main(int argc, char *argv[])
{char he[20];FILE *infile = fopen("t.txt", "rt");fread(he, sizeof(char), 20, infile);echo(he);fclose(infile);infile = fopen("b.txt", "rt");fread(he, sizeof(char), 20, infile);echo(he);fclose(infile);infile = fopen("t.txt", "rb");fread(he, sizeof(char), 20, infile);echo(he);fclose(infile);infile = fopen("b.txt", "rb");fread(he, sizeof(char), 20, infile);echo(he);fclose(infile);return 0;
}第一行,以文本方式打開(kāi)t.txt,fopen會(huì)將0d0a替換為0a
第二行,以文本方式打開(kāi)b.txt,返回原內(nèi)容
第三行,以二進(jìn)制方式打開(kāi)t.txt,fopen不作替換,直接讀取0d0a
第四行,以二進(jìn)制方式打開(kāi)b.txt,返回原內(nèi)容
轉(zhuǎn)載于:https://www.cnblogs.com/silyvin/archive/2012/12/10/9106904.html
總結(jié)
以上是生活随笔為你收集整理的fopen 中 按文本读写与按二进制读写 实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一个网站需要多少钱啊?
- 下一篇: 沈阳做胃镜要预约吗?