【发布时间】:2022-01-09 20:05:45
【问题描述】:
我想从文件中读取数据,我需要以二进制形式打开文件并一次读取数据块?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char* argv[])
{
int n;
FILE * fp;
size_t nbyte;
unsigned char * buffer[1024];
fp=open("file_test.txt",O_RDONLY);
read(fp,buffer,1);
printf("%s\n",buffer[0]);
close(fp);
return 0;
}
【问题讨论】:
-
您查看过文档或学习资料吗?基本上你需要
open、read和close。另请阅读:How to Ask -
Documentation - 包括示例。
-
unsigned char * buffer[1024]->unsigned char buffer[1024]。除此之外:你不检查open是否成功,你只读取一个字节。