zoukankan      html  css  js  c++  java
  • 文件操作总结

    1. 打开文件

    函数定义:FILE *fopen(char *pname, char *mode)

    函数说明:pname 是文件名,mode 是打开文件的方式( r:只读,w:只写)

    返回值:若返回 NULL,则打开失败。

    2. 关闭文件

    函数定义:int fclose(FILE *stream)

    函数说明:stream 为指向 FILE 对象的指针

    返回值:若返回 0,则关闭成功。

    3. 读取一个字符

    函数定义:int fgetc(FILE *stream)

    函数说明:stream 为指向 FILE 对象的指针

    返回值:若返回 EOF,则读取失败。

    4. 写一个字符到文件中

    函数定义:int fputc(int char, FILE *stream)

    函数说明:char 为要写入的字符,以整形传递;stream 为指向 FILE 对象的指针

    返回值:若返回 EOF ,则写入失败。

    下面使用上面函数实现功能,将一个文件的内容复制到另一个文件中去。代码如下:

     1 /*
     2     文件操作:将一个文件的内容复制到另一个文件中去 
     3 */
     4 
     5 #include <stdio.h>
     6 
     7 int main() {
     8     int ch;
     9     FILE *in, *out;            // 指向输入、输出文件的指针
    10     // 文件打开失败 
    11     if((in=fopen("in.txt", "r"))==NULL || (out=fopen("out.txt", "w"))==NULL) {
    12         return;
    13     } 
    14     ch = fgetc(in);            // 输入一个字符 
    15     while(ch != EOF) {
    16         fputc(ch, out);        // 写入一个字符 
    17         ch = fgetc(in);        // 继续输入 
    18     }
    19     fclose(in);                // 关闭文件流 
    20     fclose(out);
    21 
    22     return 0;
    23 }

    5. 读取一行字符串

    函数定义:char *fgets(char *str, int n, FILE *stream)

    函数说明:str 储存输入的字符串,n 表示读取 n-1 个字符,stream 为指向 FILE 对象的指针

    返回值:若返回 NULL,则表示没有任何输入。

    6. 往文件中写格式化数据

    函数定义:int fprintf(FILE *stream, const char *format, ...)

    函数说明:与 printf 函数差不多,stream 为指向 FILE 对象的指针

    返回值:如果成功,则返回写入的字符总数,否则返回一个负数。

    7. 读取格式化数据

    函数定义:int fscanf(FILE *stream, const char *format, ...)

    函数说明:与 scanf函数差不多,stream 为指向 FILE 对象的指针

    返回值:如果成功,该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误,则返回 EOF。

    用上面介绍的函数可实现文件的格式化输入和输出,代码如下:

     1 /*
     2     文件操作:文件的格式化输入和输出 
     3 */
     4 
     5 #include <stdio.h>
     6 
     7 int main() {
     8     int id, age;        // 号次、年龄 
     9     char name[10];        // 姓名 
    10     FILE *in, *out;        // 指向输入、输出文件的指针
    11     // 打开文件失败 
    12     if((in=fopen("in.txt", "r"))==NULL || (out=fopen("out.txt", "w"))==NULL) {
    13         return;
    14     }
    15     // 按格式输入 
    16     while(fscanf(in, "%d %s %d", &id, name, &age) != EOF) {
    17         // 按格式输出 
    18         fprintf(out, "id = %04d, name = %s, age = %02d
    ", id, name, age);
    19     }
    20 
    21     return 0;
    22 }

    8.  判断文件结束

    函数定义:int feof(FILE *stream)

    函数说明:stream 为指向 FILE 对象的指针

    返回值:当设置了与流关联的文件结束标识符时,该函数返回一个非零值,否则返回零。

    测试代码如下:

     1 /*
     2     文件操作:feof 
     3 */
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <stdlib.h>
     9 #include <time.h>
    10 #include <stdbool.h>
    11 
    12 int main() {
    13     char str[512];
    14     FILE* in;
    15     // 文件打开失败 
    16     if((in=fopen("in.txt", "r")) == NULL) {
    17         return;
    18     }
    19     while(!feof(in)) {
    20         fgets(str, 512, in);    // 输入一整行 
    21         printf("%s", str);        // 输出 
    22     }
    23 
    24     return 0;
    25 }
  • 相关阅读:
    Linux 修改 root密码
    python实现 CI/CD(jenkins+gitlab)
    redis集群
    土木工程材料0732
    C语言程序设计【1032】
    汽车文化【1196】
    应用写作0045
    思想道德修养与法律基础[1053]
    英语【0002】
    社区管理【0272】
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/Csummary02.html
Copyright © 2011-2022 走看看