mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-08 05:03:17 +08:00
5-8
This commit is contained in:
25
Coding/Examples/22 输入&输出/02 检查文件是否可以打开/canopen.c
Normal file
25
Coding/Examples/22 输入&输出/02 检查文件是否可以打开/canopen.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
FILE* fp;
|
||||
|
||||
if (argc != 2) {
|
||||
printf("usage: canopen filename\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((fp = fopen(argv[1], "r")) == NULL) {
|
||||
printf("%s can't be opend.\n", argv[1]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("%s can be opend.\n", argv[1]);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
Coding/Examples/22 输入&输出/02 检查文件是否可以打开/readme.md
Normal file
23
Coding/Examples/22 输入&输出/02 检查文件是否可以打开/readme.md
Normal file
@@ -0,0 +1,23 @@
|
||||
#### 程序:检查文件是否可以打开
|
||||
|
||||
下面程序判断文件是否存在,如果存在是否可以打开进行读入。在运行程序时,用户给出要检查的文件的名字:
|
||||
|
||||
```c
|
||||
canopen file
|
||||
```
|
||||
|
||||
然后程序将显示 :
|
||||
|
||||
```
|
||||
file can be opend.
|
||||
或
|
||||
file can't be opend.
|
||||
```
|
||||
|
||||
如果在命令行中录入的实际参数的数量不对,那么程序将显示出消息:
|
||||
|
||||
```
|
||||
usage: canopen filename
|
||||
```
|
||||
|
||||
来提示用户 canopen 需要一个文件名。
|
||||
34
Coding/Examples/22 输入&输出/03 复制文件/fcopy.c
Normal file
34
Coding/Examples/22 输入&输出/03 复制文件/fcopy.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
FILE* src_fp, * dest_fp;
|
||||
int ch;
|
||||
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "usage: fcopy source dest\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((src_fp = fopen(argv[1], "rb")) == NULL) {
|
||||
fprintf(stderr, "Can't open file %s\n", argv[1]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((dest_fp = fopen(argv[2], "wb")) == NULL) {
|
||||
fprintf(stderr, "Can't open file %s\n", argv[2]);
|
||||
fclose(src_fp);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while ((ch = getc(src_fp)) != EOF)
|
||||
putc(ch, dest_fp);
|
||||
|
||||
fclose(src_fp);
|
||||
fclose(dest_fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Coding/Examples/22 输入&输出/03 复制文件/readme.md
Normal file
9
Coding/Examples/22 输入&输出/03 复制文件/readme.md
Normal file
@@ -0,0 +1,9 @@
|
||||
#### 程序:复制文件
|
||||
|
||||
下面的程序用来进行文件的复制操作。当程序执行时,在命令行上指定原始文件名和新文件名。例如,将文件 f1.c 复制给文件 f2.c,使用命令:
|
||||
|
||||
```c
|
||||
fcopy f1.c f2.c
|
||||
```
|
||||
|
||||
如果命令行上的文件名不是两个,或者至少有一个文件无法打开,那么程序 fcopy 都将产出出错消息。
|
||||
37
Coding/Examples/22 输入&输出/04 修改零件记录文件/invclear.c
Normal file
37
Coding/Examples/22 输入&输出/04 修改零件记录文件/invclear.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
#define NAME_LEN 25
|
||||
#define MAX_PARTS 100
|
||||
|
||||
struct part {
|
||||
int number;
|
||||
char name[NAME_LEN + 1];
|
||||
int on_hand;
|
||||
}inventory[MAX_PARTS];
|
||||
|
||||
int num_part;
|
||||
|
||||
int main() {
|
||||
|
||||
FILE* fp;
|
||||
int i;
|
||||
|
||||
if ((fp = fopen("inventory.dat", "rb+")) == NULL) {
|
||||
fprintf(stderr, "Can't open inventory file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
num_part = fread(inventory, sizeof(struct part), MAX_PARTS, fp);
|
||||
|
||||
for (i = 0; i < num_part; i++)
|
||||
inventory[i].on_hand = 0;
|
||||
|
||||
rewind(fp);
|
||||
// 这里调用 rewind 函数是很关键的。在调用完 fread 函数之后,文件位置是在文件的末尾。如果不调用 rewind 就调用 fwrite ,那么 fwrite 将会在文件末尾添加新数据,而不会覆盖旧数据。
|
||||
fwrite(inventory, sizeof(struct part), num_part, fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
3
Coding/Examples/22 输入&输出/04 修改零件记录文件/readme.md
Normal file
3
Coding/Examples/22 输入&输出/04 修改零件记录文件/readme.md
Normal file
@@ -0,0 +1,3 @@
|
||||
#### 程序:修改零件记录文件
|
||||
|
||||
下面这个程序打开包含 part 结构的二进制文件,把结构读到数组中,把每个结构的成员 on_hand 置为 0,然后再把此结构写回到文件中。注意,程序使用 `"rb+"`模式打开文件,因此既可以读又可以写。
|
||||
Reference in New Issue
Block a user