Learning About Buffers
-
Reference link for learning about buffers: http://c.biancheng.net/view/vip_1797.html
-
Categories: fully buffered, line buffered, unbuffered
-
Fully buffered
# 定义
缓冲区填满之后,才进行输入输出操作。
# 举例
当我们将数据写入文件后,打开文件并不能立即看到内容,只有清空缓冲区(即将缓冲区中的数据“倒出”),或者关闭文件、关闭程序之后,才能在文件中看到内容。
- Line buffered
# 定义
在输入和输出过程中遇到换行符时,才进行输入和输出操作。
# 举例
标准输入设备(键盘)、标准输出设备(显示器)
- Unbuffered
# 定义
不带缓冲区,数据没有地方缓存,就立即进行输入输出。
- To eliminate various strange behaviors caused by buffers at the source, clearing the buffer before input and output is advocated here
# 参考链接
http://c.biancheng.net/view/vip_1799.html
Advanced scanf Usage
-
Reference link: http://c.biancheng.net/view/vip_1800.html
-
Using regular expressions in the scanf() function is pretty awesome!!!
-
A solution for clearing the buffer with scanf(): scanf("%*[^\n]"); scanf("%*c");
# scanf()控制字符串完整写法总结:
%{*} {width} type
# 注:{}表示可选项
- type表示读取什么类型数据,比如%d,%c,%s,%[a-z],%[^\n]等,必须存在该选项
- width表示最大读取宽度,可有可无
- *表示丢弃读取到的数据,可有可无
String-Handling Functions
-
C provides a rich set of string-handling functions that can perform operations such as input, output, concatenation, modification, comparison, copying, conversion, and searching on strings.
-
Documentation reference for string.h: http://www.cplusplus.com/reference/cstring/
Learning Preprocessor Commands
-
Commands beginning with #, such as #include and #define, are called preprocessor commands.
-
C: source program -> preprocessing -> compilation (compiled into object files) -> linking (combining multiple compiled object files and system libraries, components, and so on into an executable program)
-
C provides various preprocessing features: macro definitions, file inclusion, conditional compilation, and so on
-
#define is called a macro definition command. A so-called macro uses an identifier to represent a string. For example: #define N 100 => this piece of code is a macro definition.
# 注意
用宏定义表示数据类型和用typedef定义数据说明符的区别:
宏定义只是简单的字符串替换,由预处理器来处理;
而typedef是在编译阶段由编译器处理,且它不是简单的字符串替换,而是给原有的数据类型起一个新的名字,是一种新的数据类型。
File Operations
- In C, hardware devices can be regarded as files. Some input/output functions do not require you to specify which file to read or write because the system has already set default files for them. Of course, these can also be changed; for example, printf() can be made to output data to a file on disk.
- File operation process: open the file -> read or write the file -> close the file.
- File stream: files on disk must be loaded into memory before they can be processed, while data in memory must be written to files on disk so that it will not be lost. A file stream is the process of transferring data between a file on disk and memory, similar to water flowing from one place to another.
- Input stream: data flows from a disk file into memory; output stream: data flows from memory to a disk file.
- Of course, there are things besides disk files (this is only an analogy; there are also databases, keyboards, and networks)
- Opening a file means opening a stream.
# fopen()函数具体用法参考链接:
http://c.biancheng.net/view/2054.html
- What is the difference between opening a file in text mode and binary mode with fopen()?
没有本质区别,只是对于换行符处理不同;
总结来说:
对于windows平台,最好用"t"来打开文本文件,然后用"b"来打开二进制文件。
而对于linux平台,无所谓。
- Learn about reading and writing files below:
// 以字符形式读写文件涉及相关函数如下:
// 参考链接:http://c.biancheng.net/view/2068.html
int fgetc(FILE *fp);
int fputc(int ch, FILE *fp);
// 以字符串形式读写文件涉及相关函数如下:
// 参考链接:http://c.biancheng.net/view/2070.html
char *fgets(char *str, int n, FILE *fp);
int fputs(char *str, FILE *fp);
// 以数据块的形式读写文件涉及相关函数如下:
// 注意:在windows系统上,以数据块的形式读写文件时,需要将文件的打开方式指定为b,即以二进制方式打开文件。
// 参考链接:http://c.biancheng.net/view/2071.html
size_t fread(void *ptr, size_t size, size_t count, FILE *fp);
size_t fwrite(void *ptr, size_t size, size_t count, FILE *fp);
// 格式化读写文件涉及相关函数如下:
// 参考链接:http://c.biancheng.net/view/2073.html
int fscanf(FILE *fp, char *format, ...);
int fprintf(FILE *fp, char *format, ...);
// 随机读写文件设计函数如下:
// 参考链接:http://c.biancheng.net/view/2075.html
// 在实际开发过程中会涉及到读写文件的中间部分,要解决这个问题,首先得移动文件内部的位置指针,再进行读写。这种读写方式称为随机读写。
// 实现随机读写的关键是要按要求移动位置指针,这称为文件的定位。
void rewind(FILE *fp);
int fseek(FILE *fp, long offset, int origin);
How Do You Suppress Visual Studio Warnings If You Want to Use Unsafe Functions?
// 文件开头添加如下预处理语句即可:
#pragma warning(disable : 4996)
// 参考链接如下:
https://stackoverflow.com/questions/19321804/this-function-or-variable-may-be-unsafe-visual-studio/36521657