1:C语言文件IO
C语言中的文件操作函数如下:
文件操作函数 | 功能 |
|---|---|
fopen | 打开文件 |
fclose | 关闭文件 |
fputc | 写入一个字符 |
fgetc | 读取一个字符 |
fputs | 写入一个字符串 |
fgets | 读取一个字符串 |
fprintf | 格式化写入数据 |
fscanf | 格式化读取数据 |
fwrite | 向二进制文件写入数据 |
fread | 从二进制文件读取数据 |
fseek | 设置文件指针的位置 |
ftell | 计算当前文件指针相对于起始位置的偏移量 |
rewind | 设置文件指针到文件的起始位置 |
ferror | 判断文件操作过程中是否发生错误 |
feof | 判断文件指针是否读取到文件末尾 |
下面只会选择性对C语言的部分文件操作函数进行使用,若想详细了解其余文件操作函数的使用方法,请跳转到博主的其它博客:文件处理不再难:带你轻松攻克C语言文件操作_c语言大文件处理-CSDN博客
代码语言:javascript
AI代码解释
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> int main() { FILE * fp = fopen("log.txt","w"); if(NULL == fp) { perror("fopen"); exit(-1); } fclose(fp); fp = NULL; return 0; }运行程序后,在当前路径下就会生成对应文件.
那么我们也可以通过输出重定向向文件里面写入新的内容.
1.1:对文件的初步理解
那么有了上面的简单例子我们可以对文件有一个简单的初步理解
- 打开文件,本质是进程在打开文件.
- 文件在没有被打开的时候,在什么地方上------->在磁盘上头.
- 进程能够同时打开很多文件.
系统中可以存在很多进程------------>很多情况下,OS内部,一定存在大量的被打开的文件---------->那么OS就要对这些被打开的文件进行处理,先描述,再组织------->因此可以推测,每一个被打开的文件在OS内部,一定要存在对应的描述文件属性的结构体,类似与PCB.
www.dongchedi.com/article/7594897166766965272
www.dongchedi.com/article/7594897310665245246
www.dongchedi.com/article/7594897192310407705
www.dongchedi.com/article/7594897631617794585
www.dongchedi.com/article/7594896580080435737
www.dongchedi.com/article/7594895704896684568
www.dongchedi.com/article/7594897374330503705
www.dongchedi.com/article/7594895618720449048
www.dongchedi.com/article/7594896034183397912
www.dongchedi.com/article/7594896087656694334
www.dongchedi.com/article/7595300698343260734
www.dongchedi.com/article/7595300086620734014
www.dongchedi.com/article/7595300794661257753
www.dongchedi.com/article/7595300922285146649
www.dongchedi.com/article/7595300794661159449
www.dongchedi.com/article/7595301535278858814
www.dongchedi.com/article/7595300053246837310
www.dongchedi.com/article/7595298997771141656
www.dongchedi.com/article/7595298747215987224
www.dongchedi.com/article/7595298653444080190
www.dongchedi.com/article/7595299173835276862
www.dongchedi.com/article/7595293834620191257
www.dongchedi.com/article/7595294214674596377
www.dongchedi.com/article/7595294735703966270
www.dongchedi.com/article/7595294214674825753
www.dongchedi.com/article/7595269939515703870
www.dongchedi.com/article/7595270106293600793
www.dongchedi.com/article/7595268901785584153
www.dongchedi.com/article/7595268784974545433
www.dongchedi.com/article/7595270592459506238
www.dongchedi.com/article/7595268816629219865
www.dongchedi.com/article/7595267640395235902
www.dongchedi.com/article/7595267822872166937
www.dongchedi.com/article/7595266120056422936
www.dongchedi.com/article/7595265326464795161
www.dongchedi.com/article/7595264479055970878
www.dongchedi.com/article/7595263814942376472
www.dongchedi.com/article/7595263889965924926
www.dongchedi.com/article/7595264219466121753
www.dongchedi.com/article/7595261441830486590
www.dongchedi.com/article/7595257155528409624
www.dongchedi.com/article/7595257148431974937
www.dongchedi.com/article/7595255577883198014
www.dongchedi.com/article/7595257264999858713
www.dongchedi.com/article/7595255131160085054
www.dongchedi.com/article/7595254038942401086
www.dongchedi.com/article/7595272535034430014
www.dongchedi.com/article/7595273235844137496
www.dongchedi.com/article/7595271162356302398
www.dongchedi.com/article/7595269071357231641
www.dongchedi.com/article/7595269220477567512
www.dongchedi.com/article/7595268287076958782
www.dongchedi.com/article/7595269071357264409
www.dongchedi.com/article/7595268315833270809
www.dongchedi.com/article/7595265492903363096
www.dongchedi.com/article/7595265055567299096
www.dongchedi.com/article/7595265773019841049
www.dongchedi.com/article/7595265891500114494
www.dongchedi.com/article/7595264904798978585
www.dongchedi.com/article/7595263429863195161
www.dongchedi.com/article/7595261995696800280
www.dongchedi.com/article/7595258440394818073
www.dongchedi.com/article/7595256795065893401
www.dongchedi.com/article/7595256961541767704
www.dongchedi.com/article/7595257257731408446
www.dongchedi.com/article/7595253896583774745
www.dongchedi.com/article/7595252944199778841