关于c:Programming-abstractions-in-C阅读笔记p139p143

2次阅读

共计 2183 个字符,预计需要花费 6 分钟才能阅读完成。

《Programming Abstractions In C》学习第 55 天,p139-p140,总结如下:

一、技术总结

1. 文件 I / O 操作

文件 I / O 操作能够分为一下这些步骤:

(1) 申明文件指针对象。

File *infile;

(2) 关上文件

fopen()。关上文件的模式有“r”, “w”, “a” 三种模式。

(3) 传输数据

读取文件的形式能够是 character by character(getc()/putc()),也能够是 line by line(fget()/fput())。

(4) 敞开文件

fclose()。

2. 文件 I / O 操作示例:复制文件

#include <stdio.h>
#include <stdbool.h> // for bool, true, false data type
#include <stdlib.h> // for exit()

void CopyRemovingComments(FILE *infile, FILE *outfile);

int main() {
    // 申明文件指针对象
    FILE *infile, *outfile;
    char *infileName, *outfileName;

    /*
     * 关上文件:fopen()
     * 如果文件不存在,则返回 NULL,所以须要查看
     */
    infileName = "D:\\CProject\\chater3.4\\jabber.txt"; // 这里应用的是绝对路径,也能够应用相对路径
    outfileName = "D:\\CProject\\chater3.4\\jabbercopy.txt";
    infile = fopen(infileName, "r");
    if (infile == NULL) {printf("Cannot open input file: %s \n", infileName);
        exit(0);
    }

    /*
     * 传输数据
     * 传输数据有很多种形式,例如 chracter by character(getc/putc),line by line(fget/fput, ReadLine)
     * 为了解决 stdio.h 存在的一些问题,作者对 stdio 进行了封装,封装后失去的的是 simpio
     */
    outfile = fopen(outfileName, "w");
    if (outfile == NULL) {printf("Cannot open output file: %s \n", outfileName);
        exit(0);
    }

    CopyRemovingComments(infile, outfile);

    /*
     * 敞开文件
     */
    fclose(infile);
    fclose(outfile);
    printf("Copying is completed");

    return 0;
}

void CopyRemovingComments(FILE *infile, FILE *outfile) {
    int ch, nch;
    bool commentFlag; // 这里应用的是 stdbool.h 接口中的 bool

    commentFlag = false; // 这里应用的是 stdbool.h 接口中的 false, 书中应用的是封装后的 FALSE

    while ((ch = getc(infile)) != EOF) {if (commentFlag) {if (ch == '*') {nch = getc(infile); //
                if (nch == '/') {commentFlag = false;} else {ungetc(nch, infile);
                }
            }
        } else {if (ch == '/') {nch = getc(infile);
                if (nch == '*') {commentFlag = true;} else {ungetc(nch, infile);
                }
            }
            if (!commentFlag) {putc(ch, outfile);
            }
        }
    }
}

二、英语总结

1.endpoint 什么意思?

答:c.the end of sth(起点)。

2.transfer 什么意思?

答:transfer 也是一个在计算机相关材料中常常看到的词。p140, For an input file, the function read data from the file into your program; for an output file, the function transfer data from the program to the file。数据从文件到程序中,或者从程序中到文件,即是一种 transfer。通过该例句,对 tranfer 有一个形象的理解。

3.intermix 什么意思?

答:

(1) 解释:vi/vt. to combine two or more different things。

(2) 搭配:intermix sth with sth。

(3) 例句:p140, Doing so allows you to intermix numeric data with strings and other data types。

三、参考资料

1. 编程

(1)Eric S.Roberts,《Programming Abstractions in C》:https://book.douban.com/subject/2003414

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridage Dictionary:https://dictionary.cambridge.org

欢送搜寻及关注:编程人 (a_codists)

正文完
 0