026. 在文件中定位

在C语言中,文件定位是指在文件中移动读写指针到特定的位置。这通常通过fseekftellrewind等函数来实现。这些函数允许你在文件中进行随机访问,即可以跳到文件的任意位置进行读写操作。

1. fseek函数

fseek函数用于移动文件指针到指定的位置。其原型如下:

int fseek(FILE *stream, long offset, int whence);
  • stream:文件指针。

  • offset:偏移量,表示从指定位置移动的字节数。

  • whence:指定偏移量的基准位置,常见的值包括:

  • SEEK_SET:文件开头(0)。

  • SEEK_CUR:当前位置。

  • SEEK_END:文件末尾。

示例1:使用fseek定位到文件的第10个字节

#include <stdio.h>

int main() {
    FILE *file;

    // 打开文件
    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open the file");
        return 1;
    }

    // 定位到文件的第10个字节
    if (fseek(file, 10, SEEK_SET) != 0) {
        perror("Failed to seek");
        fclose(file);
        return 1;
    }

    // 读取当前位置的字符
    char ch;
    if (fread(&ch, sizeof(char), 1, file) == 1) {
        printf("Character at position 10: %c\n", ch);
    } else {
        perror("Failed to read");
    }

    // 关闭文件
    fclose(file);
    return 0;
}

输出结果

假设文件example.txt的内容为"Hello, World!",输出结果为:

Character at position 10: W

2. ftell函数

ftell函数用于获取当前文件指针的位置。其原型如下:

long ftell(FILE *stream);
  • stream:文件指针。

  • 返回值:当前文件指针的位置(从文件开头算起的字节数)。

示例2:使用ftell获取当前文件指针位置

#include <stdio.h>

int main() {
    FILE *file;

    // 打开文件
    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open the file");
        return 1;
    }

    // 定位到文件的第10个字节
    if (fseek(file, 10, SEEK_SET) != 0) {
        perror("Failed to seek");
        fclose(file);
        return 1;
    }

    // 获取当前文件指针位置
    long position = ftell(file);
    printf("Current file pointer position: %ld\n", position);

    // 关闭文件
    fclose(file);
    return 0;
}

输出结果

假设文件example.txt的内容为"Hello, World!",输出结果为:

Current file pointer position: 10

3. rewind函数

rewind函数用于将文件指针重置到文件的开头。其原型如下:

void rewind(FILE *stream);
  • stream:文件指针。

示例3:使用rewind重置文件指针

#include <stdio.h>

int main() {
    FILE *file;

    // 打开文件
    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open the file");
        return 1;
    }

    // 定位到文件的第10个字节
    if (fseek(file, 10, SEEK_SET) != 0) {
        perror("Failed to seek");
        fclose(file);
        return 1;
    }

    // 重置文件指针到文件开头
    rewind(file);

    // 读取文件的第一个字符
    char ch;
    if (fread(&ch, sizeof(char), 1, file) == 1) {
        printf("First character in the file: %c\n", ch);
    } else {
        perror("Failed to read");
    }

    // 关闭文件
    fclose(file);
    return 0;
}

输出结果

假设文件example.txt的内容为"Hello, World!",输出结果为:

First character in the file: H

4. 综合示例:读取文件的特定部分

以下示例展示了如何使用fseekftell读取文件的特定部分。

示例4:读取文件的第10到20个字节

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file;
    char buffer[11]; // 用于存储读取的内容,额外一个字节用于存储'\0'

    // 打开文件
    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open the file");
        return 1;
    }

    // 定位到文件的第10个字节
    if (fseek(file, 10, SEEK_SET) != 0) {
        perror("Failed to seek");
        fclose(file);
        return 1;
    }

    // 读取从第10到20个字节的内容
    if (fread(buffer, sizeof(char), 10, file) != 10) {
        perror("Failed to read");
        fclose(file);
        return 1;
    }

    // 添加字符串结束符
    buffer[10] = '\0';

    printf("Content from position 10 to 20: %s\n", buffer);

    // 关闭文件
    fclose(file);
    return 0;
}

输出结果

假设文件example.txt的内容为"Hello, World! This is a test file.",输出结果为:

Content from position 10 to 20: World! Thi

通过上述示例,你可以看到如何在C语言中进行文件定位:

  1. fseek:用于移动文件指针到指定的位置。
  2. ftell:用于获取当前文件指针的位置。
  3. rewind:用于将文件指针重置到文件的开头。

视频讲解

BiliBili: 视睿网络-哔哩哔哩视频 (bilibili.com)