文章目录

  • 文本文件和二进制文件的处理
  • 文件
  • 键盘和屏幕作用为文本流
  • 换行与EOF
  • 文件指针变量
  • 图示
  • 二进制文件
  • 更高效率的文件操作
  • 二进制的优缺点

文本文件和二进制文件的处理

  • 标准输入
  • 标准输出
  • 程序控制

文件

  • 文本文件,即在外存上的已命名的字符集合

    • 自动在最后加一个特殊字符,即文件结束符,标记为EOF

      • 不妨表示为<eof>
    • 按下return or enter键时,会在文件中放置一个换行符 \n
This is a text file<newline>
It has two lines.<newline><eof>
  • 每一行都以<newline>为结束
  • 每一个文件都以<eof>为结束
  • 而在磁盘中,它是连续的存储空间占用,如下
This is a text file<newline>It has two lines.<newline><eof>
  • 即文本输入流,是字符代码的连续流,只不过有些代码表示换行或是结束

    • 流可用于输入【来自键盘】
    • 可用于输出【给到屏幕,或是打印机,或是文件】
    • 可用于文件读写
      • input stream
      • output stream

键盘和屏幕作用为文本流

  • 交互编程

    • 用户有输入,从键盘输入

      • stdin
    • 系统有输出,输出到屏幕上
      • stdout
    • 如果出错,也显示到屏幕上
      • stderr
  • 这些都是文本流,即都是由一个个字符构成的,纯字符
  • 在交互编程时,使用一个标记值指示数据输入的结束,而不是放置eof,不过eof也可以使用
    • 单个键不能代表eof
    • 使用ctrl+D可以在unix上表示eof
    • 使用ctrl+Z可以在windows上表示eof

换行与EOF

  • <newline>标记一行文本的结束
  • <eof>标记整个文件的结束
// 循环处理输入
// 但这里没有检测:输入成功与否,只是循环的条件是出现EOF才退出,否则一直做“输入”和“处理”
int status;
int num;
for (status = scanf("%d", &num); status != EOF; status = scanf("%d", &num))
{process(num);  // 处理
}

文件指针变量

  • 库:stdio.h

  • 函数:fopen()可以准备好fp

    • 字符串表示文件名

    • 字符串表示打开模式

      • 只读
      • 只写
      • 读写
      • 追加
  • 如果用"w"模式打开一个文件,之前这个文件如果已经存在,则通常会引起文件内容的丢失

  • 也就是说,打开一个文件时,OS会自动为文件版本编号,并产生一个新版本,那么文件的内容就不会丢失,这时,就要用“a”模式打开

#include <stdio.h>
#define STASIZ 80int main(void)
{char inName[STRSIZ], outName[STRSIZ];FILE *inp, *outp;char ch;printf("Enter name of file you wanna backup > ");// 打开读文件:直到打开文件成功,注意for循环的ABCD四个部分,打开成功,才会退出for循环for(scanf("%s",inName);                     // A(inp = fopen("inName","r"))==NULL;       // Bscanf("%s",inName))                       // C{                                           // Dprintf("Connot open %s for input\n",inName);printf("Re-enter file name > ");}// 打开写文件:直到打开文件成功for(scanf("%s",outName);(inp = fopen("outName","w"))==NULL;scanf("%s",outName)){printf("Connot open %s for input\n",outName);printf("Re-enter file name > ");}    // 实现逐个字符读写for( ch = getc(inp); ch != EOF; ch = getc(inp))   // 一直读到文件尾{putc(ch,outp);   // 输出到文件}// 关闭文件fclose(inp);fclose(outp);printf("Copied %s to %s.\n", inName, outName);return 0;
}

图示

#mermaid-svg-B2vjO70QBeaL4TFx .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-B2vjO70QBeaL4TFx .label text{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .node rect,#mermaid-svg-B2vjO70QBeaL4TFx .node circle,#mermaid-svg-B2vjO70QBeaL4TFx .node ellipse,#mermaid-svg-B2vjO70QBeaL4TFx .node polygon,#mermaid-svg-B2vjO70QBeaL4TFx .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-B2vjO70QBeaL4TFx .node .label{text-align:center;fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .node.clickable{cursor:pointer}#mermaid-svg-B2vjO70QBeaL4TFx .arrowheadPath{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-B2vjO70QBeaL4TFx .flowchart-link{stroke:#333;fill:none}#mermaid-svg-B2vjO70QBeaL4TFx .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-B2vjO70QBeaL4TFx .edgeLabel rect{opacity:0.9}#mermaid-svg-B2vjO70QBeaL4TFx .edgeLabel span{color:#333}#mermaid-svg-B2vjO70QBeaL4TFx .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-B2vjO70QBeaL4TFx .cluster text{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-B2vjO70QBeaL4TFx .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-B2vjO70QBeaL4TFx text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-B2vjO70QBeaL4TFx .actor-line{stroke:grey}#mermaid-svg-B2vjO70QBeaL4TFx .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-B2vjO70QBeaL4TFx .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-B2vjO70QBeaL4TFx #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-B2vjO70QBeaL4TFx .sequenceNumber{fill:#fff}#mermaid-svg-B2vjO70QBeaL4TFx #sequencenumber{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx #crosshead path{fill:#333;stroke:#333}#mermaid-svg-B2vjO70QBeaL4TFx .messageText{fill:#333;stroke:#333}#mermaid-svg-B2vjO70QBeaL4TFx .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-B2vjO70QBeaL4TFx .labelText,#mermaid-svg-B2vjO70QBeaL4TFx .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-B2vjO70QBeaL4TFx .loopText,#mermaid-svg-B2vjO70QBeaL4TFx .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-B2vjO70QBeaL4TFx .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-B2vjO70QBeaL4TFx .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-B2vjO70QBeaL4TFx .noteText,#mermaid-svg-B2vjO70QBeaL4TFx .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-B2vjO70QBeaL4TFx .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-B2vjO70QBeaL4TFx .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-B2vjO70QBeaL4TFx .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-B2vjO70QBeaL4TFx .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .section{stroke:none;opacity:0.2}#mermaid-svg-B2vjO70QBeaL4TFx .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-B2vjO70QBeaL4TFx .section2{fill:#fff400}#mermaid-svg-B2vjO70QBeaL4TFx .section1,#mermaid-svg-B2vjO70QBeaL4TFx .section3{fill:#fff;opacity:0.2}#mermaid-svg-B2vjO70QBeaL4TFx .sectionTitle0{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .sectionTitle1{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .sectionTitle2{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .sectionTitle3{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-B2vjO70QBeaL4TFx .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .grid path{stroke-width:0}#mermaid-svg-B2vjO70QBeaL4TFx .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-B2vjO70QBeaL4TFx .task{stroke-width:2}#mermaid-svg-B2vjO70QBeaL4TFx .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .taskText:not([font-size]){font-size:11px}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-B2vjO70QBeaL4TFx .task.clickable{cursor:pointer}#mermaid-svg-B2vjO70QBeaL4TFx .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-B2vjO70QBeaL4TFx .taskText0,#mermaid-svg-B2vjO70QBeaL4TFx .taskText1,#mermaid-svg-B2vjO70QBeaL4TFx .taskText2,#mermaid-svg-B2vjO70QBeaL4TFx .taskText3{fill:#fff}#mermaid-svg-B2vjO70QBeaL4TFx .task0,#mermaid-svg-B2vjO70QBeaL4TFx .task1,#mermaid-svg-B2vjO70QBeaL4TFx .task2,#mermaid-svg-B2vjO70QBeaL4TFx .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutside0,#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutside2{fill:#000}#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutside1,#mermaid-svg-B2vjO70QBeaL4TFx .taskTextOutside3{fill:#000}#mermaid-svg-B2vjO70QBeaL4TFx .active0,#mermaid-svg-B2vjO70QBeaL4TFx .active1,#mermaid-svg-B2vjO70QBeaL4TFx .active2,#mermaid-svg-B2vjO70QBeaL4TFx .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-B2vjO70QBeaL4TFx .activeText0,#mermaid-svg-B2vjO70QBeaL4TFx .activeText1,#mermaid-svg-B2vjO70QBeaL4TFx .activeText2,#mermaid-svg-B2vjO70QBeaL4TFx .activeText3{fill:#000 !important}#mermaid-svg-B2vjO70QBeaL4TFx .done0,#mermaid-svg-B2vjO70QBeaL4TFx .done1,#mermaid-svg-B2vjO70QBeaL4TFx .done2,#mermaid-svg-B2vjO70QBeaL4TFx .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-B2vjO70QBeaL4TFx .doneText0,#mermaid-svg-B2vjO70QBeaL4TFx .doneText1,#mermaid-svg-B2vjO70QBeaL4TFx .doneText2,#mermaid-svg-B2vjO70QBeaL4TFx .doneText3{fill:#000 !important}#mermaid-svg-B2vjO70QBeaL4TFx .crit0,#mermaid-svg-B2vjO70QBeaL4TFx .crit1,#mermaid-svg-B2vjO70QBeaL4TFx .crit2,#mermaid-svg-B2vjO70QBeaL4TFx .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-B2vjO70QBeaL4TFx .activeCrit0,#mermaid-svg-B2vjO70QBeaL4TFx .activeCrit1,#mermaid-svg-B2vjO70QBeaL4TFx .activeCrit2,#mermaid-svg-B2vjO70QBeaL4TFx .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-B2vjO70QBeaL4TFx .doneCrit0,#mermaid-svg-B2vjO70QBeaL4TFx .doneCrit1,#mermaid-svg-B2vjO70QBeaL4TFx .doneCrit2,#mermaid-svg-B2vjO70QBeaL4TFx .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-B2vjO70QBeaL4TFx .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-B2vjO70QBeaL4TFx .milestoneText{font-style:italic}#mermaid-svg-B2vjO70QBeaL4TFx .doneCritText0,#mermaid-svg-B2vjO70QBeaL4TFx .doneCritText1,#mermaid-svg-B2vjO70QBeaL4TFx .doneCritText2,#mermaid-svg-B2vjO70QBeaL4TFx .doneCritText3{fill:#000 !important}#mermaid-svg-B2vjO70QBeaL4TFx .activeCritText0,#mermaid-svg-B2vjO70QBeaL4TFx .activeCritText1,#mermaid-svg-B2vjO70QBeaL4TFx .activeCritText2,#mermaid-svg-B2vjO70QBeaL4TFx .activeCritText3{fill:#000 !important}#mermaid-svg-B2vjO70QBeaL4TFx .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-B2vjO70QBeaL4TFx g.classGroup text .title{font-weight:bolder}#mermaid-svg-B2vjO70QBeaL4TFx g.clickable{cursor:pointer}#mermaid-svg-B2vjO70QBeaL4TFx g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-B2vjO70QBeaL4TFx g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-B2vjO70QBeaL4TFx .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-B2vjO70QBeaL4TFx .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-B2vjO70QBeaL4TFx .dashed-line{stroke-dasharray:3}#mermaid-svg-B2vjO70QBeaL4TFx #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx .commit-id,#mermaid-svg-B2vjO70QBeaL4TFx .commit-msg,#mermaid-svg-B2vjO70QBeaL4TFx .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-B2vjO70QBeaL4TFx g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-B2vjO70QBeaL4TFx g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-B2vjO70QBeaL4TFx g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-B2vjO70QBeaL4TFx .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-B2vjO70QBeaL4TFx .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-B2vjO70QBeaL4TFx .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-B2vjO70QBeaL4TFx .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-B2vjO70QBeaL4TFx .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-B2vjO70QBeaL4TFx .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-B2vjO70QBeaL4TFx .edgeLabel text{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-B2vjO70QBeaL4TFx .node circle.state-start{fill:black;stroke:black}#mermaid-svg-B2vjO70QBeaL4TFx .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-B2vjO70QBeaL4TFx #statediagram-barbEnd{fill:#9370db}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-state .divider{stroke:#9370db}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-B2vjO70QBeaL4TFx .note-edge{stroke-dasharray:5}#mermaid-svg-B2vjO70QBeaL4TFx .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-B2vjO70QBeaL4TFx .error-icon{fill:#522}#mermaid-svg-B2vjO70QBeaL4TFx .error-text{fill:#522;stroke:#522}#mermaid-svg-B2vjO70QBeaL4TFx .edge-thickness-normal{stroke-width:2px}#mermaid-svg-B2vjO70QBeaL4TFx .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-B2vjO70QBeaL4TFx .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-B2vjO70QBeaL4TFx .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-B2vjO70QBeaL4TFx .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-B2vjO70QBeaL4TFx .marker{fill:#333}#mermaid-svg-B2vjO70QBeaL4TFx .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-B2vjO70QBeaL4TFx {color: rgba(0, 0, 0, 0.75);font: ;}

读文件
程序
写文件
#mermaid-svg-C6gCXkwW622yyriQ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-C6gCXkwW622yyriQ .label text{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .node rect,#mermaid-svg-C6gCXkwW622yyriQ .node circle,#mermaid-svg-C6gCXkwW622yyriQ .node ellipse,#mermaid-svg-C6gCXkwW622yyriQ .node polygon,#mermaid-svg-C6gCXkwW622yyriQ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-C6gCXkwW622yyriQ .node .label{text-align:center;fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .node.clickable{cursor:pointer}#mermaid-svg-C6gCXkwW622yyriQ .arrowheadPath{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-C6gCXkwW622yyriQ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-C6gCXkwW622yyriQ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-C6gCXkwW622yyriQ .edgeLabel rect{opacity:0.9}#mermaid-svg-C6gCXkwW622yyriQ .edgeLabel span{color:#333}#mermaid-svg-C6gCXkwW622yyriQ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-C6gCXkwW622yyriQ .cluster text{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-C6gCXkwW622yyriQ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-C6gCXkwW622yyriQ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-C6gCXkwW622yyriQ .actor-line{stroke:grey}#mermaid-svg-C6gCXkwW622yyriQ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-C6gCXkwW622yyriQ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-C6gCXkwW622yyriQ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-C6gCXkwW622yyriQ .sequenceNumber{fill:#fff}#mermaid-svg-C6gCXkwW622yyriQ #sequencenumber{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-C6gCXkwW622yyriQ .messageText{fill:#333;stroke:#333}#mermaid-svg-C6gCXkwW622yyriQ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-C6gCXkwW622yyriQ .labelText,#mermaid-svg-C6gCXkwW622yyriQ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-C6gCXkwW622yyriQ .loopText,#mermaid-svg-C6gCXkwW622yyriQ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-C6gCXkwW622yyriQ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-C6gCXkwW622yyriQ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-C6gCXkwW622yyriQ .noteText,#mermaid-svg-C6gCXkwW622yyriQ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-C6gCXkwW622yyriQ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-C6gCXkwW622yyriQ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-C6gCXkwW622yyriQ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-C6gCXkwW622yyriQ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .section{stroke:none;opacity:0.2}#mermaid-svg-C6gCXkwW622yyriQ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-C6gCXkwW622yyriQ .section2{fill:#fff400}#mermaid-svg-C6gCXkwW622yyriQ .section1,#mermaid-svg-C6gCXkwW622yyriQ .section3{fill:#fff;opacity:0.2}#mermaid-svg-C6gCXkwW622yyriQ .sectionTitle0{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .sectionTitle1{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .sectionTitle2{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .sectionTitle3{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-C6gCXkwW622yyriQ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .grid path{stroke-width:0}#mermaid-svg-C6gCXkwW622yyriQ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-C6gCXkwW622yyriQ .task{stroke-width:2}#mermaid-svg-C6gCXkwW622yyriQ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .taskText:not([font-size]){font-size:11px}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-C6gCXkwW622yyriQ .task.clickable{cursor:pointer}#mermaid-svg-C6gCXkwW622yyriQ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-C6gCXkwW622yyriQ .taskText0,#mermaid-svg-C6gCXkwW622yyriQ .taskText1,#mermaid-svg-C6gCXkwW622yyriQ .taskText2,#mermaid-svg-C6gCXkwW622yyriQ .taskText3{fill:#fff}#mermaid-svg-C6gCXkwW622yyriQ .task0,#mermaid-svg-C6gCXkwW622yyriQ .task1,#mermaid-svg-C6gCXkwW622yyriQ .task2,#mermaid-svg-C6gCXkwW622yyriQ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutside0,#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutside2{fill:#000}#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutside1,#mermaid-svg-C6gCXkwW622yyriQ .taskTextOutside3{fill:#000}#mermaid-svg-C6gCXkwW622yyriQ .active0,#mermaid-svg-C6gCXkwW622yyriQ .active1,#mermaid-svg-C6gCXkwW622yyriQ .active2,#mermaid-svg-C6gCXkwW622yyriQ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-C6gCXkwW622yyriQ .activeText0,#mermaid-svg-C6gCXkwW622yyriQ .activeText1,#mermaid-svg-C6gCXkwW622yyriQ .activeText2,#mermaid-svg-C6gCXkwW622yyriQ .activeText3{fill:#000 !important}#mermaid-svg-C6gCXkwW622yyriQ .done0,#mermaid-svg-C6gCXkwW622yyriQ .done1,#mermaid-svg-C6gCXkwW622yyriQ .done2,#mermaid-svg-C6gCXkwW622yyriQ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-C6gCXkwW622yyriQ .doneText0,#mermaid-svg-C6gCXkwW622yyriQ .doneText1,#mermaid-svg-C6gCXkwW622yyriQ .doneText2,#mermaid-svg-C6gCXkwW622yyriQ .doneText3{fill:#000 !important}#mermaid-svg-C6gCXkwW622yyriQ .crit0,#mermaid-svg-C6gCXkwW622yyriQ .crit1,#mermaid-svg-C6gCXkwW622yyriQ .crit2,#mermaid-svg-C6gCXkwW622yyriQ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-C6gCXkwW622yyriQ .activeCrit0,#mermaid-svg-C6gCXkwW622yyriQ .activeCrit1,#mermaid-svg-C6gCXkwW622yyriQ .activeCrit2,#mermaid-svg-C6gCXkwW622yyriQ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-C6gCXkwW622yyriQ .doneCrit0,#mermaid-svg-C6gCXkwW622yyriQ .doneCrit1,#mermaid-svg-C6gCXkwW622yyriQ .doneCrit2,#mermaid-svg-C6gCXkwW622yyriQ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-C6gCXkwW622yyriQ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-C6gCXkwW622yyriQ .milestoneText{font-style:italic}#mermaid-svg-C6gCXkwW622yyriQ .doneCritText0,#mermaid-svg-C6gCXkwW622yyriQ .doneCritText1,#mermaid-svg-C6gCXkwW622yyriQ .doneCritText2,#mermaid-svg-C6gCXkwW622yyriQ .doneCritText3{fill:#000 !important}#mermaid-svg-C6gCXkwW622yyriQ .activeCritText0,#mermaid-svg-C6gCXkwW622yyriQ .activeCritText1,#mermaid-svg-C6gCXkwW622yyriQ .activeCritText2,#mermaid-svg-C6gCXkwW622yyriQ .activeCritText3{fill:#000 !important}#mermaid-svg-C6gCXkwW622yyriQ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-C6gCXkwW622yyriQ g.classGroup text .title{font-weight:bolder}#mermaid-svg-C6gCXkwW622yyriQ g.clickable{cursor:pointer}#mermaid-svg-C6gCXkwW622yyriQ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-C6gCXkwW622yyriQ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-C6gCXkwW622yyriQ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-C6gCXkwW622yyriQ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-C6gCXkwW622yyriQ .dashed-line{stroke-dasharray:3}#mermaid-svg-C6gCXkwW622yyriQ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ .commit-id,#mermaid-svg-C6gCXkwW622yyriQ .commit-msg,#mermaid-svg-C6gCXkwW622yyriQ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-C6gCXkwW622yyriQ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-C6gCXkwW622yyriQ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-C6gCXkwW622yyriQ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-C6gCXkwW622yyriQ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-C6gCXkwW622yyriQ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-C6gCXkwW622yyriQ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-C6gCXkwW622yyriQ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-C6gCXkwW622yyriQ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-C6gCXkwW622yyriQ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-C6gCXkwW622yyriQ .edgeLabel text{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-C6gCXkwW622yyriQ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-C6gCXkwW622yyriQ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-C6gCXkwW622yyriQ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-C6gCXkwW622yyriQ .note-edge{stroke-dasharray:5}#mermaid-svg-C6gCXkwW622yyriQ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-C6gCXkwW622yyriQ .error-icon{fill:#522}#mermaid-svg-C6gCXkwW622yyriQ .error-text{fill:#522;stroke:#522}#mermaid-svg-C6gCXkwW622yyriQ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-C6gCXkwW622yyriQ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-C6gCXkwW622yyriQ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-C6gCXkwW622yyriQ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-C6gCXkwW622yyriQ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-C6gCXkwW622yyriQ .marker{fill:#333}#mermaid-svg-C6gCXkwW622yyriQ .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-C6gCXkwW622yyriQ {color: rgba(0, 0, 0, 0.75);font: ;}

键盘
程序
屏幕

二进制文件

  • 输入文本时,人按键,以为是输入了对应的符号,但本质上是,ASCII键值,对应到机器中是二进制编码;
  • 当文本文件存储时,程序要用资源来将文本字符流转为二进制,这才是它在内存中的存在形式;
  • 当读取时,程序要用资源来将二进制再转为文本字符流,这是人可以读懂的显示形式;
  • 而scanf和printf就是来完成这些功能的
    • 字符,数字,符号
    • 整数
    • 浮点数
      • 尾数
      • 指数
  • 为避免不必要的转来转去,可以直接使用二进制进行存取文件操作

更高效率的文件操作

  • 直接通过程序产生二进制文件
  • 再直接将文件中的各个数据项的计算机内部表示形式存储在文件中
FILE *bfp;
int i;
// 打开文件
bfp = fopen("nums.bin","wb");// 写文件:将500内的偶数存入
for(i=0;i<=500;i+=2)
{fwrite(&i, sizeof(int), 1, bfp);// 块方式,一次写一个整数[的存储空间大小],即4个字节
}// 关闭文件
fclose(bfp);

二进制的优缺点

  • 优点:

    • 高效
    • 在操作double时,不用担心在文本操作时转化为字符串时精度上的丢失
    • 二进制无格式,操作统一
  • 缺点:
    • 在一台计算机上产生的二进制文件,在另一种型号的计算机上,几乎无法读取!!!
    • 所以xml和JSON大行其道啊

20201207-C语言-文本文件和二进制文件的处理相关推荐

  1. C语言文本文件与二进制文件转换

    本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件前255字节以内的字符转换成相应的AscII码值的二进制表示,并存入输出文件a2.txt中.然后再将二进制文件还原并存入a3.txt ...

  2. 基于存储的C语言文件操作常规问题分析(文本文件与二进制文件)

    基于存储的C语言文件操作常规问题分析(文本文件与二进制文件) 问题描述 文本文件与二进制文件 数据写入文本乱码问题 fopen和open 流式文件操作常用函数 直接I/O文件操作常用函数 问题描述 我 ...

  3. C语言基础14——文件操作。文本文件或二进制文件读写。通讯录的改造。文件缓冲区讲解

    目录 为什么使用文件? 什么是文件? 文件的打开和关闭 文件指针 文件的打开和关闭 文件的打开方式 流 重定义文件 文件流 文件的顺序读写 以字符形式读写文本文件 fputc()函数 fgetc()函 ...

  4. c语言实现指定路径文件读取_C语言实现文件复制功能(包括文本文件和二进制文件)...

    文件的复制是常用的功能,要求写一段代码,让用户输入要复制的文件以及新建的文件,然后对文件进行复制.能够复制的文件包括文本文件和二进制文件,你可以复制1G的电影,也可以复制1Byte的txt文档.实现文 ...

  5. 第十一章 文件操作_C语言实现文件复制功能(包括文本文件和二进制文件)

    文件的复制是常用的功能,要求写一段代码,让用户输入要复制的文件以及新建的文件,然后对文件进行复制.能够复制的文件包括文本文件和二进制文件,你可以复制1G的电影,也可以复制1Byte的txt文档. 实现 ...

  6. 文本文件和二进制文件的区别

    广义的二进制文件即指文件,由文件在外部设备的存放形式为二进制而得名.狭义的二进制文件即除文本文件以外的文件.文本文件是一种由很多行字符构成的计算机文件.文本文件存在于计算机系统中,通常在文本文件最后一 ...

  7. Python文件操作-文本文件、二进制文件、csv文件的读取写入、OS、shutil、CSV模块、常用字符编码

    Python文件操作 文本文件和二进制文件 文件操作相关模块 open()创建文件对象 文件对象的常用属性和方法 pickle 序列化 文本文件读取和写入 文本文件写入步骤 write()/write ...

  8. 【文本文件与二进制文件的区别;文件打开与关闭的方法、不同读写方式】(学习笔记19--文件上)

    目录 文件的打开与关闭 文件的打开 文件的关闭 标准文件流 文件流的重定向 文件的读写 以字符的方式读写文件 以行的方式读写文件 以格式化的方式读写文件 以块的方式读写文件 文件的打开与关闭 文件的实 ...

  9. 文本文件和二进制文件详解(转)

    转自:http://www.cnblogs.com/pengwangguoyh/articles/3223072.html 前言: 1)文本文件:这类文件以文本的ASCII码形式存储在计算机中.它是以 ...

最新文章

  1. 管理7k+工作流,月运行超10000万次,Lyft开源的Flyte平台意味着什么?
  2. linux服务器远程桌面 数字键盘不能用
  3. Newtonsoft.Json使用
  4. Ubuntu 软件包管理详解
  5. Linux进程 excel族函数的用法
  6. 【IDEA工具设置】解决控制台中文输出乱码问题
  7. 编码原则:如何减少缩进层次
  8. 基于51单片机的数码管显示方案
  9. 程序员如何才配拥有姓名?
  10. 初次远程做Linux Iptables规则注意事项
  11. Linux下的段错误产生的原因及调试方法-转
  12. Linux使用——Linux命令——CentOS7防火墙使用
  13. 密码日记本密码忘记了怎样打开
  14. Android SystemUI下拉状态栏添加快捷开关
  15. 一本好书,若干能源大数据分析论文分享
  16. ACRCloud音乐识别python3版SDK,acrcloud_extr_tool.so导入报错
  17. ESLint配置详解
  18. GLES2.0中文API-glGetActiveUniform
  19. 我是如何学习的,分享本人的学习方法
  20. word转pdf出现错误 解决方法

热门文章

  1. DDPG笔记(归纳总结)
  2. nginx简单配置代理服务器
  3. 微信小程序生成页面分享二维码(代码亲测有效)
  4. 重磅推荐:建大数据平台太难了!给我发个工程原型吧!
  5. spring 计划会议
  6. 怎样通过heic图片转换器,将heic图片转换jpg方法
  7. 在桌面计算机找不到光盘驱动,升级win10后光驱不能用找不到该怎么办?
  8. Excel 从身份证号码上获取出生日期和年龄
  9. webgl绘制图形API——drawArrays、drawElements
  10. 韦东山衔接班——1.4_u-boot分析之源码第二阶段