我喜欢打开一个二进制文件,然后喜欢检查一些标头,并且在读取之后,我喜欢通过ftell()获取FilePosition,并将结构分配到共享内存中。我喜欢将文件数据循环放入该Struct中。但我收到此错误-
MWriter.c:71:43: error: invalid conversion from ‘char’ to ‘void*’ [-fpermissive]
fread(M->Data[i*BufferSize], sizeof(char), BufferSize, FP+FilePosition);
我喜欢打开一个二进制文件,然后喜欢检查一些标头,并且在读取之后,我喜欢通过ftell()获取FilePosition,并将结构分配到共享内存中。我喜欢将文件数据循环放入该Struct中。但我收到此错误-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
int main(void){
struct MemData{
char FileName[128];//POINTER PUTS DATA INTO NON-SHARED MEMORY
int LastByteLength;
int ReadPointer;
int WritePointer;
char Data[512000];//MEMORY BLOCK SIZE: 500 KB
};
int SD;
struct MemData *M;
int NumberOfBuffers=10;
int BufferSize=51200;//FILE BUFFER SIZE 50 KB
SD= shm_open("/program.shared", O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
if(SD< 0){
printf("\nshm_open() error \n");
return EXIT_FAILURE;
}
fchmod(SD, S_IRWXU|S_IRWXG|S_IRWXO);
if(ftruncate(SD, sizeof(MemData))< 0){
printf ("ftruncate() error \n");
return EXIT_FAILURE;
}
//THE FOLLOWING TYPECASTING AVOIDS THE NEED TO ATTACH THROUGH shmat() in shm.h HEADER I GUESS.
M=(struct MemData*)mmap(NULL, sizeof(MemData), PROT_READ|PROT_WRITE, MAP_SHARED, SD, 0);
if(M== MAP_FAILED){
printf("mmap() error");
return EXIT_FAILURE;
}else{
strcpy(M->FileName, "xaa");
M->LastByteLength=0;
M->ReadPointer=-1;
M->WritePointer=-1;
memset(M->Data, '\0', strlen(M->Data));
}
char FileName[128]="xaa";
FILE *FP= fopen(FileName, "rb");
if(FP!= NULL){
struct stat StatBuf;
if(stat(FileName, &StatBuf)==-1){
printf("failed to fstat %s\n", FileName);
exit(EXIT_FAILURE);
}
long long FileSize=StatBuf.st_size;
printf("\n File Size: %lld", FileSize);
long long FilePosition=ftell(FP);
FilePosition=ftell(FP);
long long CopyableMemorySize=FileSize-FilePosition;
printf("\n Copyable File Size: %lld", CopyableMemorySize);
int NumberOfFileBuffers=CopyableMemorySize/BufferSize;
printf("\n Number Of File Buffers: %d", NumberOfFileBuffers);
for(int i=0; i<NumberOfFileBuffers; i++){
if(abs(M->WritePointer-M->ReadPointer)==NumberOfBuffers){
//WAIT
}else{
fread(M->Data[i*BufferSize], sizeof(char), BufferSize, FP+FilePosition);
}
}
fclose(FP);
}
close(SD);
return 0;
}