mmap函数问题 20
#include<unistd.h>#include<stdio.h>#include<sys/mman.h>#include<fcntl.h>#include<stdl...
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
typedef struct
{
int integer;
char string[24];
}RECORD;
#define NRECORDS (100)
int main()
{
RECORD record,*mapped;
int i,f;
FILE *fp;
fp=fopen("records.dat","w+");
for(i=0;i<NRECORDS;i++)
{
record.integer=i;
sprintf(record.string,"RECORD-%d",i);
fwrite(&record,sizeof(record),1,fp);
}
fclose(fp);
fp=fopen("records.dat","r+");
fseek(fp,43*sizeof(record),SEEK_SET);
fread(&record,sizeof(record),1,fp);
record.integer=143;
fwrite(&record,sizeof(record),1,fp);
fclose(fp);
f=open("records.dat",O_RDWR);
mapped=(RECORD *)mmap(0,NRECORDS*sizeof(record),PORT_READ|PORT_WRITE,MAP_SHARED,f,0);
mapped[43].integer=243;
sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);
msync((void *)mapped,NRECORDS*sizeof(record),MS_ASYNC);
munmap((void *)mapped,NRECORDS*sizeof(record));
close(f);
exit (0);
}
报错:
/home/sun/c/mmap/main.c||In function ‘main’:|
/home/sun/c/mmap/main.c|39|error: ‘PORT_READ’ undeclared (first use in this function)|
/home/sun/c/mmap/main.c|39|error: (Each undeclared identifier is reported only once|
/home/sun/c/mmap/main.c|39|error: for each function it appears in.)|
/home/sun/c/mmap/main.c|39|error: ‘PORT_WRITE’ undeclared (first use in this function)|
||=== Build finished: 4 errors, 0 warnings ===| 展开
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
typedef struct
{
int integer;
char string[24];
}RECORD;
#define NRECORDS (100)
int main()
{
RECORD record,*mapped;
int i,f;
FILE *fp;
fp=fopen("records.dat","w+");
for(i=0;i<NRECORDS;i++)
{
record.integer=i;
sprintf(record.string,"RECORD-%d",i);
fwrite(&record,sizeof(record),1,fp);
}
fclose(fp);
fp=fopen("records.dat","r+");
fseek(fp,43*sizeof(record),SEEK_SET);
fread(&record,sizeof(record),1,fp);
record.integer=143;
fwrite(&record,sizeof(record),1,fp);
fclose(fp);
f=open("records.dat",O_RDWR);
mapped=(RECORD *)mmap(0,NRECORDS*sizeof(record),PORT_READ|PORT_WRITE,MAP_SHARED,f,0);
mapped[43].integer=243;
sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);
msync((void *)mapped,NRECORDS*sizeof(record),MS_ASYNC);
munmap((void *)mapped,NRECORDS*sizeof(record));
close(f);
exit (0);
}
报错:
/home/sun/c/mmap/main.c||In function ‘main’:|
/home/sun/c/mmap/main.c|39|error: ‘PORT_READ’ undeclared (first use in this function)|
/home/sun/c/mmap/main.c|39|error: (Each undeclared identifier is reported only once|
/home/sun/c/mmap/main.c|39|error: for each function it appears in.)|
/home/sun/c/mmap/main.c|39|error: ‘PORT_WRITE’ undeclared (first use in this function)|
||=== Build finished: 4 errors, 0 warnings ===| 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询