C语言File文件操作函数学习

(禁止转载)

(content from 《C Primer Plus, Fifth Edition》 By Stephen Prata)


· Functions:
fopen(), getc(), putc(), exit(), fclose()
fprintf(), fscanf(), fgets(), fputs()
fread(), fwrite()


fopen()

该函数可以用来打开一个文件,同时可以根据第二参数决定对该文件的操作类型。

具体格式例如:

FILE *fp;

fp=fopen(“test.txt”,”w+x”);  //加x后,若文件存在,则打开失败,防止改写原文件

"r"

read: Open file for input operations. The file must exist.

"w"

write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

"a"

append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.

"r+"

read/update: Open a file for update (both for input and output). The file must exist.

"w+"

write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

"a+"

append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

"rb", "wb", "ab",
"ab+", "a+b", "wb+",
"w+b", "ab+", "a+b"

Like the preceding modes, except it uses binary mode instead of
text mode(二进制形式操作)

Read(读操作) write(写操作) append(添加操作)

#include <stdio.h>
#include <stdlib.h> // ANSI C exit() prototype
int main(int argc, char *argv[])
{int ch; // place to store each character as readFILE *fp; // "file pointer"long count = 0;if (argc != 2){printf("Usage: %s filename\n", argv[0]);exit(1);}if ((fp = fopen(argv[1], "r")) == NULL){printf("Can't open %s\n", argv[1]);exit(1);}while ((ch = getc(fp)) != EOF){putc(ch,stdout); // same as putchar(ch);count++;}fclose(fp);printf("File %s has %ld characters\n", argv[1], count);return 0;
}

getc() and putc()

该函数用来读取文件中的内容。

具体格式例如:

ch = getc(fpin);//read from file

putc(ch,fpout);//write to file

int ch; // int to hold EOF
FILE * fp;
fp = fopen("wacky.txt", "r");
ch = getc(fp); // get initial input
while (ch != EOF)
{putchar(ch); // process inputch = getc(fp); // get next input
}

exit() and fclose()

fclose()函数的作用是关闭文件。

具体格式例如:

fclose(fp);

exit()函数的作用为退出程序,它即使不在main函数内,也可以退出整个程序。

if (fclose(fp) != 0)printf("Error in closing file %s\n", argv[1]);

fprintf(), fscanf(), fgets(), and fputs()

fprintf(), fscanf()这两个函数的操作类似于printf()函数,只是这两个函数是写入文件。

具体使用格式例如:

fscanf(fp,"%s",words);

fprintf(fp, "%s ", words);

fgets(buf, MAX, fp);//buf是一个数组,fp是文件指针,MAX为字符串的最大长度

fputs(buf, fp);

#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{FILE *fp;char words[MAX];if ((fp = fopen("wordy", "a+")) == NULL){fprintf(stdout,"Can't open \"words\" file.\n");exit(1);}puts("Enter words to add to the file; press the Enter");puts("key at the beginning of a line to terminate.");while (gets(words) != NULL && words[0] != '\0')fprintf(fp, "%s ", words);puts("File contents:");rewind(fp); /* go back to beginning of file */while (fscanf(fp,"%s",words) == 1)puts(words);if (fclose(fp) != 0)fprintf(stderr,"Error closing file\n");return 0;
}
#include <stdio.h>
#define MAXLINE 20
int main(void)
{char line[MAXLINE];while (fgets(line, MAXLINE, stdin) != NULL && line[0] != '\n')fputs(line, stdout);return 0;
}

fread() and fwrite()

这两个函数提供二进制操作。

具体使用格式例如:

double earnings[10];
fread(earnings, sizeof (double), 10, fp);

char buffer[256];
fwrite(buffer, 256, 1, fp);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(void)
{FILE *fa, *fs;// fa for append file, fs for source fileint files = 0; // number of files appendedchar file_app[SLEN]; // name of append filechar file_src[SLEN]; // name of source fileputs("Enter name of destination file:");gets(file_app);if ((fa = fopen(file_app, "a")) == NULL){fprintf(stderr, "Can't open %s\n", file_app);exit(2);}if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0){fputs("Can't create output buffer\n", stderr);exit(3);}puts("Enter name of first source file (empty line to quit):");while (gets(file_src) && file_src[0] != '\0'){if (strcmp(file_src, file_app) == 0)fputs("Can't append file to itself\n",stderr);else if ((fs = fopen(file_src, "r")) == NULL)fprintf(stderr, "Can't open %s\n", file_src);else{if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0){fputs("Can't create input buffer\n",stderr);continue;}append(fs, fa);if (ferror(fs) != 0)fprintf(stderr,"Error in reading file %s.\n",file_src);if (ferror(fa) != 0)fprintf(stderr,"Error in writing file %s.\n",file_app);fclose(fs);files++;printf("File %s appended.\n", file_src);puts("Next file (empty line to quit):");}}printf("Done. %d files appended.\n", files);fclose(fa);return 0;
}
void append(FILE *source, FILE *dest)
{size_t bytes;static char temp[BUFSIZE]; // allocate oncewhile ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)fwrite(temp, sizeof (char), bytes, dest);
}

C语言File文件操作函数学习相关推荐

  1. C语言程序设计 文件操作函数

    文件操作函数 C语言 (FILE fputc fgetc fputs fgets fscanf fprintf) 在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别 ...

  2. c语言全文件操作函数,C语言文件操作函数大全

    C语言 文件操作函数大全 C语言文件操作函数 2007-10-17 19:21 13.1C语言文件 1,两种文件存取方式(输入,输出方式) 顺序存取 直接存取 2,数据的两种存放形式 文本文件 二进制 ...

  3. C语言 File文件处理 读文件

    在C语言程序开发中,也会遇到很多文件上传,文件写入等对于文件的操作业务需要开发,文件处理也是任何应用程序的重要组成部分.C语言有几种创建,读取,更新和删除文件的方法.本文主要介绍C语言 File文件操 ...

  4. linux c文件操作,C语言文件操作(FILE)与常用文件操作函数

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

  5. 文件操作(FILE)与常用文件操作函数——C语言

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

  6. C语言 文件操作 深度解析 #重点知识:文件操作函数的使用#

    文章目录 前言 1. 为什么使用文件 2. 什么是文件 程序文件 数据文件 3. 文件的打开和关闭 4. 文件的顺序读写 `fgetc` `fputc` `fgets` `fputs` `fprint ...

  7. [C语言]文件操作函数

    [C语言]文件操作函数 ​ 本文主要学习**fopen,fclose,fgetc,fgets,fputc,fputs,fwrite,fread,feof**这几个文件操作函数. ​ 以上函数,需要导入 ...

  8. 【C 语言】文件操作 ( 按照单个字符的方式读写文件 | fgetc 函数 | fputc 函数 )

    文章目录 一.文件名路径设置 二.文件打开方式 三.fputc 函数 | 按照字符方式写文件 1.fputc 函数 2.代码示例 四.fgetc 函数 | 按照字符方式读文件 1.fgetc 函数 2 ...

  9. C语言文件操作函数大全(看到总结的真的很好,就转载贡献给大家了)

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof 表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * st ...

最新文章

  1. s6-8 TCP 拥塞控制
  2. 《闪耀暖暖》可增加“心之门”爆闪耀的方法?网友:巧合罢了
  3. python基础期末考试_python基础试题(4)
  4. vb 读取mysql所有表名_vb怎么列举出一个mdb数据库里面所有表名?
  5. Nagios---NRPE
  6. PHP单次数据库查询实现无限级分类
  7. 在ie6下文字颜色不兼容的解决方法
  8. python 01列表异或_python运算符及优先级顺序
  9. 为什么 Oracle 应该主推 NetBeans
  10. Atitit 依赖管理之道 1. 概念 依赖管理,是指在什么地方以什么形式引入外部代码。 1 1.1.1. 理解模块化和依赖管理: 1 1.2. 依赖管理,有三个层面。 单一职责原则,协议对象引用,
  11. 从数学的视角看社交网络
  12. 非接触式IC卡的分类(三)
  13. 图解医学影像纹理特征
  14. Codeforces Round #633 (Div. 2) C. Powered Addition
  15. 高职高考计算机一级证,3+证书计算机一级证书难不难,都考哪些内容?
  16. win10 ie设置不了代理服务器
  17. 传说之下手机如何使用debug_传说之下手机版怎么设置按键 按键设置方法
  18. docker 清理磁盘
  19. Dubbo后台管理和监控中心部署
  20. 动态规划(DP)算法介绍

热门文章

  1. java做笛卡尔积的两种方式
  2. ubuntu18.04忘记密码的解决办法
  3. 函数式编程中的战斗机(二) --运用elm语言MUV设计模式做一个简单的应用实例
  4. python中延时函数_python – 如何在Python中延迟时间?
  5. 怎样使用EDIUS里面的抠像功能
  6. MIUI又出大BUG,把10年米粉整破防了
  7. 操作系统面试问答题大全
  8. css 背景图旋转 只让背景图片旋转180度的实现示例
  9. EasyDSS RTMP流媒体解决方案之Windows服务安装方案
  10. 南京工业大学计算机科学与技术分数线,南京工业大学计算机科学与技术专业2016年在江苏理科高考录取最低分数线...