Linux 下 C语言大文件读写(大于4G)
以下的做法整理自论坛上的帖子。
如何create大文件
要大就非常大,1T吧。
有两种方法:
一.dd
dd if=/dev/zero of=1T.img bs=1G seek=1024 count=0
bs=1G表示每一次读写1G数据,count=0表示读写0次,seek=1024表示略过1024个Block不写,前面block size是1G,所以共略过1T!
这是创建大型sparse文件最简单的方法。
二.ftruncate64/ftruncate
如果用系统函数就稍微有些麻烦,因为涉及到宏的问题。我会结合一个实际例子详细说明,其中OPTION标志的就是测试项。
文件sparse.c:
//OPTION 1:是否定义与大文件相关的宏
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILENAME "bigfile"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
int fd, ret;
off_t offset;
int total = 0;
if ( argc >= 2 )
{
total = atol(argv[1]);
printf("total=%d\n", total);
}
//OPTION 2:是否有O_LARGEFILE选项
//fd = open(FILENAME, O_RDWR|O_CREAT|O_LARGEFILE, 0644);
fd = open(FILENAME, O_RDWR|O_CREAT, 0644);
if (fd < 0) {
perror(FILENAME);
return -1;
}
offset = (off_t)total *1024ll*1024ll*1024ll;
printf("offset=%ld\n", offset);
//OPTION 3:是否调用64位系统函数
//if (ftruncate64(fd, offset) < 0)
if (ftruncate(fd, offset) < 0)
{
printf("[%d]-ftruncate64 error: %s\n", errno, strerror(errno));
close(fd);
return 0;
}
close(fd);
printf("OK\n");
return 0;
}
测试环境:
linux:/disk/test/big # gcc --version
gcc (GCC) 3.3.5 20050117 (prerelease) (SUSE Linux)
linux:/disk/test/big # uname -a
Linux linux 2.6.11.4-20a-default #1 Wed Mar 23 21:52:37 UTC 2005 i686 i686 i386 GNU/Linux
测试结果
相关文档:
总览
用iptables -ADC 来指定链的规
则
,-A添加 -D删除 -C 修改
iptables - [RI] chain rule num rule-specification[option]
用iptables - RI 通过规则的顺序指定
iptables -D chain rule num[option]
删除指定规则
iptables -[LFZ] [chain][option]
用iptables -LFZ 链名 [选项]
iptables -[NX] chain
用 -NX ......
pid_t pid=fork()
it has 3 situation for the return result pid
0 child
>0 parent process
<0 fork fail
fork create a new process and it parent live alse when the child process had been created ......
在制定标准时,
C89
委员会关注下列几个原则,这些原则直到今天还在指导我们考虑问题。最重要的几个原则如下:
现存代码很重要,而现存的
C
编译器实现并不重要。
C
代码能够是可移植的。
C
代码可以是不可移植的。
C89
委员会不希望阻止
C
程序员写机器专用代码,因为这是
C
的一个强项。这造成了严格一 ......
1. 使用TCHAR类型,定义在tchar.h中
#include <tchar.h>
#include <stdio.h>
int main()
{
TCHAR s[] = "你";
printf("%s \n",s);
return 0;
}
2.关于C++中文字符的处理
一 引入问题
代码 wchar_t a[3]=L”中国”,编译时出错,出错信息为:数组越界。但wchar_ ......
在此摘录C编译时出现的警告信息的意义。
1) warning: ISO C90 forbids mixed declarations and code
C语言是面向过程的语言,这个警告通常表示声明应该在其他代码的前面。
2) warning: initialization from incompatible pointer type
在Linux kernel中有许多callback函数,这个警告表明callback函数的实现中,或者返 ......