Re: [問題] 關於sys call-open() read() write()..
※ 引述《mayasky ( )》之銘言:
: 標題: [問題] 關於sys call-open() read() write()..
: 時間: Thu Nov 16 00:26:04 2006
:
: 由於我需要低階直接存取功能,我不使用C的LIB去做檔案操作
: 我直接用open與read.write
: 但始終有Illegal seek
: 但是檔案又寫的進去讀得出來(在不同程式)
:
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 140.114.71.192
: ※ 編輯: mayasky 來自: 140.114.71.192 (11/16 00:26)
: → qrtt1:有沒有範例能看 :P 11/16 11:06
殘念,由於安全問題,新的KERNEL都不准open配上O_DIRECT了,
不過我還是附上直接用sys call呼叫的讀寫
//============================write============================
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
typedef struct _people
{
char name[10];
int high;
}people;
int main()
{
people jack;
strcpy(jack.name,"jack");
jack.high=180;
people rose=
{
.name="rose",
.high=190,
};
people parray[]={jack,rose};
char *err;
int wdb=open("dbfile2",O_CREAT | O_WRONLY);
perror(err);
write(wdb,parray,sizeof(parray));
perror(err);
close(wdb);
return 0;
}
//=========================read==================================
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/unistd.h>
#include <sys/types.h>
#include <errno.h>
typedef struct _people
{
char name[10];
int high;
}people;
int main()
{
char *err;
people rdata;
int rdb=open("dbfile2",O_RDONLY);
perror(err);
read(rdb,&rdata,sizeof(people));
perror(err);
printf("name: %s high:%d\n",&rdata.name,rdata.high);
read(rdb,&rdata,sizeof(people));
printf("name: %s high:%d\n",&rdata.name,rdata.high);
close(rdb);
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.71.192
※ 編輯: mayasky 來自: 140.114.71.192 (11/16 15:27)
推
11/17 08:04, , 1F
11/17 08:04, 1F
→
11/18 03:36, , 2F
11/18 03:36, 2F
→
11/18 14:01, , 3F
11/18 14:01, 3F
討論串 (同標題文章)
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章