0
我想写一个简单的程序,通过封装函数如open
,lseek
,pread
读取文件。尝试从文件读取使用文件描述符打印数字和斜线到控制台
我的测试文件中包含:
first second third forth fifth sixth
seventh eighth
我试图主函数读取20个字节,从文件偏移10:
#include <iostream>
#include "CacheFS.h"
using namespace std;
int main(int argc, const char * argv[]) {
char * filename1 = "/Users/Desktop/File";
int fd1 = CacheFS_open(filename1);
//read from file and print it
void* buf[20];
CacheFS_pread(fd1, &buf, 20, 10);
cout << (char*)buf << endl;
}
实施的,主要是使用功能:
int CacheFS_open(const char *pathname)
{
mode_t modes = O_SYNC | 0 | O_RDONLY;
int fd = open(pathname, modes);
return fd;
}
int CacheFS_pread(int file_id, void *buf, size_t count, off_t offset)
{
off_t seek = lseek(file_id, offset, SEEK_SET);
off_t fileLength = lseek(file_id, 0, SEEK_END);
if (count + seek <= fileLength) //this case we are not getting to the file end when readin this chunk
{
pread(file_id, &buf, count, seek);
} else { //count is too big so we can only read a part of the chunk
off_t size = fileLength - seek;
pread(file_id, &buf, size, seek);
}
return 0;
}
我的主要功能打印到控制台:
\350\366\277_\377
我期望它从文件本身打印一些值,而不是一些数字和斜杠代表我不了解的东西。 这是为什么发生?
我实现了你在3中写的(从buf调用读取中删除&),它工作。我采取的其他点作为额外的有用的提示。谢谢!! – Eyzuky
缓冲区需要在堆栈或堆上存在。 'pread'函数将不会为你分配它。如果您忽略写入堆栈或堆损坏后不想写入的位置。 –
我将按照你的说明实现初始化,谢谢先生。 – Eyzuky