C语言文件截取操作源码

include

include

int main() {

FILE source_file, target_file;

long start_offset, end_offset;

char ch;

// 打开源文件
source_file = fopen("source.txt", "r");
if (source_file == NULL) {
    perror("Error opening source file");
    return -1;
}

// 打开目标文件
target_file = fopen("target.txt", "w");
if (target_file == NULL) {
    perror("Error opening target file");
    return -1;
}

// 设置起始和结束偏移
start_offset = 10;
end_offset = 100;

// 定位到起始偏移
fseek(source_file, start_offset, SEEK_SET);

// 复制数据直到结束偏移
while ((ch = fgetc(source_file)) != EOF && ftell(source_file) <= end_offset) {
    fputc(ch, target_file);
}

// 关闭文件
fclose(source_file);
fclose(target_file);

return 0;

}

rar 文件大小:186.95KB