linux c 读写超过2G的大文件解决办法

linux默认环境下打开、读、写超过2G的文件会返回错误。

定义如下宏可以突破这个限制,对read/write和fread/fwrite同时有效。

注意它必须定义在所有头文件之前。

#define _FILE_OFFSET_BITS 64

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>

 

查了查书,看到了linux的ext2文件大小的限制。根据ext2的结构一共是三级索引,文件最大可达12KB+256KB+64MB+16GB。呵呵,linux操作系统是32位操作系统文件大小只能是4G。呵呵
好像以前的版本均有2G限制
不好,最近我用LINUX DESKTOP/SERVER 3.0版,开发商说已突破此限制。
linux现在没有限制了,6.2的好像有.

转自:
http://macwader.blog.hexun.com/23704425_d.html

如何解决linux下文件大小的限制

    在linux下,用fwrite等C API函数来写文件时,会有一个文件大小的限制,一般是2G。问题是在做mysql数据库备份时发现

的。在网上搜了一下,确实有人碰到跟我一样的问题。解决的方案写的并不是很详细。不过有人贴出一个fseeko的文档说明出来,

里面有很重要的信息就是,其中fseeko函数中的off_t在大部分平台上默认为4字节大小。但是可以通过#define _FILE_OFFSET_BITS

64来把off_t这个转化为64比特类型的大小。其实也可以在linux下man fseeko来查看。
    虽然有这个信息,但是并不知道怎么用,第一反应在我的“头文件”中加入#define _FILE_OFFSET_BITS 64,重新编译一次

,实验无效。突然想到mysqldump这个命令是可以写一个超过2G大小的文件,查看了一下mysqldump的源代码。发现其实应该是在编

译时加入编译选项-D_FILE_OFFSET_BITS=64就可以解决此问题。
实例:
// example.c
#include <stdio.h>
int main(int argc, char* argv[])
{
    FILE *fp;
    if ((fp = fopen("test.dat", "w+")) == NULL)
        return;
    int a = 0;
    char data[1024] = "";
    while (true)
    {
        fwrite(data, sizeof(data), 1, fp);
        a++;
        if (a >= 1024*2049)
            break;
    }
    fclose(fp);
}
编译:gcc -g -o example -D_FILE_OFFSET_BITS=64 example.c
问题解决!!!

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C语言中,读写配置文件通常使用INI文件格式。INI文件格式是一种简单的文本文件格式,其中键值对以节(section)的形式组织,并使用方括号([])将节名括起来。 以下是一个示例INI文件: ``` [Section1] key1=value1 key2=value2 [Section2] key3=value3 key4=value4 ``` 使用C语言读取INI文件的步骤如下: 1. 打开INI文件并读入内容。 2. 解析INI文件内容,将键值对存储到内存中的数据结构中。 3. 使用存储的键值对执行相应的操作。 以下是一个简单的例子: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_LEN 1024 #define MAX_SECTION_LEN 256 #define MAX_KEY_LEN 256 #define MAX_VALUE_LEN 256 typedef struct { char section[MAX_SECTION_LEN]; char key[MAX_KEY_LEN]; char value[MAX_VALUE_LEN]; } config_item_t; int parse_config_file(const char* filename, config_item_t** items, int* count) { FILE* fp = fopen(filename, "r"); if (!fp) { return -1; } char line[MAX_LINE_LEN]; config_item_t* item = NULL; int item_count = 0; while (fgets(line, MAX_LINE_LEN, fp)) { // 去掉行末换行符 char* p = strchr(line, '\n'); if (p) *p = '\0'; // 去掉行首空格 p = line; while (*p == ' ') ++p; // 解析节 if (*p == '[') { char* q = strchr(p, ']'); if (!q) { fclose(fp); return -1; } *q = '\0'; item = NULL; continue; } // 解析键值对 if (item) { char* q = strchr(p, '='); if (!q) continue; // 忽略无效行 *q = '\0'; strcpy(item->key, p); strcpy(item->value, q + 1); ++item; } else { // 新建配置项 item = (config_item_t*)realloc(item, (item_count + 1) * sizeof(config_item_t)); if (!item) { fclose(fp); return -1; } strcpy(item->section, p); item->key[0] = '\0'; item->value[0] = '\0'; items[item_count++] = item; } } fclose(fp); *count = item_count; return 0; } void free_config_items(config_item_t** items, int count) { for (int i = 0; i < count; ++i) { free(items[i]); } free(items); } int main() { config_item_t* items[100]; int count = 0; if (parse_config_file("test.ini", items, &count) == 0) { for (int i = 0; i < count; ++i) { printf("[%s]\n", items[i]->section); printf("%s=%s\n", items[i]->key, items[i]->value); } } free_config_items(items, count); return 0; } ``` 该程序使用`parse_config_file`函数读取INI文件并解析内容,将每个节的键值对存储到一个`config_item_t`结构体中。最后,在`main`函数中遍历所有配置项并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值