Programming abstractions in C阅读笔记:p139-p143

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

一、技术总结

1.文件I/O操作

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

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

c 复制代码
File *infile;

(2)打开文件

fopen()。打开文件的模式有"r", "w", "a"三种模式。

(3)传输数据

读取文件的方式可以是character by character( getc()/putc() ),也可以是line by line( fget()/fput() )。

(4)关闭文件

fclose()。

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

c 复制代码
#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)

相关推荐
Nebula_g3 分钟前
C语言应用实例:斐波那契数列与其其他应用
c语言·开发语言·后端·学习·算法
被遗忘的旋律.38 分钟前
Linux驱动开发笔记(十九)——IIC(AP3216C驱动+MPU6050驱动)
linux·驱动开发·笔记
千弥霜1 小时前
codeforces1914 C~F
c语言·算法
wyiyiyi2 小时前
【数据结构+算法】进栈顺序推算、卡特兰数与逆波兰表达式
汇编·数据结构·笔记·算法
white-persist2 小时前
汇编代码详细解释:汇编语言如何转化为对应的C语言,怎么转化为对应的C代码?
java·c语言·前端·网络·汇编·安全·网络安全
Larry_Yanan2 小时前
QML学习笔记(五十二)QML与C++交互:数据转换——时间和日期
开发语言·c++·笔记·qt·学习·ui·交互
TL滕2 小时前
从0开始学算法——第二天(时间、空间复杂度)
数据结构·笔记·学习·算法
C++chaofan3 小时前
MyBatis - Plus学习笔记
java·spring boot·笔记·后端·mysql·架构·mybatis
满天星83035773 小时前
【C++】智能指针
c语言·开发语言·c++·visual studio
旺仔老馒头.4 小时前
【数据结构与算法】手撕排序算法(二)
c语言·数据结构·算法·排序算法