Back home
中文
H7 / SECURITY RESEARCH NOTES

Chapter 13: File Input and Output

Preface

  • As a programmer, you must be able to write programs that create files and read data from and write data to files.

  • C views a file as a continuous series of bytes, each of which can be read individually.

  • C provides two file modes: text mode and binary mode

# 学会区分如下:
- 文本内容和二进制内容
如果文件最初使用二进制编码的字符(例如ASCII)表示文本,该文件就是文本文件,其中包含文本内容。
如果文件中的二进制值代表机器语言代码或数值数据或图片或音乐编码,该文件就是二进制文件,其中包含二进制内容。

- 文本文件格式和二进制文件格式


- 文件的文本模式和二进制模式
C提供两种访问文件的途径:二进制模式和文本模式。
二进制模式中,程序可以访问文件的每个字节。
而在文本模式中,程序所见的内容和文件的实际内容不同,程序以文本模式读取内容时将本地环境的行末尾或文件末尾映射为“C模式”。例如C程序在MS-DOS平台(Notepad生成MS-DOS格式的文本文件)以文本模式读取文件时,把文件中的\r\n转换成\n;写入文件时,把\n转换成\r\n。

I/O Levels

  • Divided into low-level I/O and standard high-level I/O
- 底层I/O使用操作系统提供的基本I/O服务
- 标准高级I/O使用C库的标准包和stdio.h头文件定义

注:因为无法保证所有的操作系统都使用相同的底层I/O模型,所以C标准只支持标准I/O包。

Standard Files

  • A C program automatically opens three files: standard input, standard output, and standard error output.

strcpy, strncpy, and strncpy_s

- strncpy
# Reference:https://www.cplusplus.com/reference/cstring/strncpy/
注:目前visual studio里已经用不了了,目前用strncpy_s或者strcpy

- strncpy_s
# Reference: https://rurban.github.io/safeclib/doc/safec-3.0/d2/d68/strncpy__s_8c.html#a1554d39ed8b47634296e8fb86594f77e

- strcpy

fopen_s

- fopen_s
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-s-wfopen-s?view=msvc-170

strcat_s

- strcat_s

Random Access: fseek() and ftell()

- fseek()
- ftell()

参考书 13.5.1

The fgetpos() and fsetpos() Functions