C语言文件读写常用函数

本文详细介绍了C语言中的文件操作函数,如fopen用于打开文件,fclose关闭文件,fgetc、fgets读取字符和字符串,fputc、fputs写入字符和字符串,fprintf和fscanf进行格式化输入输出,以及fseek和ftell进行文件定位。这些函数是进行文件操作的基础。
摘要由CSDN通过智能技术生成

1. fopen函数

  • 返回值:文件指针(FILE*)
  • 参数:文件名(包括文件路径)、打开模式(例如:"r"表示只读,"w"表示写入,"a"表示追加)
  • 用法:用于打开一个文件,并返回一个文件指针,以便后续的文件操作。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    // 文件操作...
    fclose(file);
    return 0;
}

2. fclose函数

  • 返回值:整数类型(0表示成功,EOF表示失败)
  • 参数:文件指针(FILE*)
  • 用法:用于关闭一个已打开的文件。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    // 文件操作...
    fclose(file);
    return 0;
}

3. fgetc函数

  • 返回值:整数类型(读取的字符的ASCII码值,读取失败返回EOF)
  • 参数:文件指针(FILE*)
  • 用法:用于从文件中读取一个字符。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    int ch;
    while ((ch = fgetc(file)) != EOF) {
        printf("%c", ch);
    }
    fclose(file);
    return 0;
}

4. fgets函数

  • 返回值:字符指针(读取的字符串,读取失败返回NULL)
  • 参数:字符数组、最大读取字符数、文件指针(FILE*)
  • 用法:用于从文件中读取一行字符串。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    char buffer[100];
    while (fgets(buffer, 100, file) != NULL) {
        printf("%s", buffer);
    }
    fclose(file);
    return 0;
}

5. fputc函数

  • 返回值:整数类型(写入的字符的ASCII码值,写入失败返回EOF)
  • 参数:字符、文件指针(FILE*)
  • 用法:用于向文件中写入一个字符。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    char ch = 'A';
    fputc(ch, file);
    fclose(file);
    return 0;
}

6. fputs函数

  • 返回值:整数类型(成功返回非负值,失败返回EOF)
  • 参数:字符串、文件指针(FILE*)
  • 用法:用于向文件中写入一个字符串。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    char *str = "Hello, World!";
    fputs(str, file);
    fclose(file);
    return 0;
}

7. fprintf函数

  • 返回值:整数类型(成功返回写入的字符数,失败返回负值)
  • 参数:文件指针(FILE*)、格式化字符串、可变参数
  • 用法:用于按照指定的格式将数据写入文件。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    int num = 10;
    fprintf(file, "The number is: %d\n", num);
    fclose(file);
    return 0;
}

8. fscanf函数

  • 返回值:整数类型(成功返回读取的项目数,失败返回负值)
  • 参数:文件指针(FILE*)、格式化字符串、可变参数
  • 用法:用于从文件中按照指定的格式读取数据。
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }
    int num;
    fscanf(file, "%d", &num);
    printf("The number is: %d\n", num);
    fclose(file);
    return 0;
}

9. fseek函数

  • 返回值:整数类型(0表示成功,非0表示失败)
  • 参数:文件指针(FILE*)、偏移量(long int)、起始位置(int)
  • 用法:用于设置文件指针的位置,实现文件的定位操作。

10. ftell函数

  • 返回值:长整型(当前文件指针的位置)
  • 参数:文件指针(FILE*)
  • 用法:用于获取文件指针的当前位置。

下面是fseek和ftell函数的示例代码:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    // 使用fseek函数定位文件指针到文件末尾
    fseek(file, 0, SEEK_END);

    // 使用ftell函数获取文件指针的当前位置
    long int size = ftell(file);

    printf("The size of the file is: %ld bytes\n", size);

    fclose(file);
    return 0;
}

在上述示例代码中,首先打开了一个文件,然后使用fseek函数将文件指针定位到文件末尾,接着使用ftell函数获取文件指针的当前位置,即文件的大小。最后关闭文件。

使用fseek函数和ftell函数可以实现文件的定位和随机读写操作。



  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值