64,651
社区成员
发帖
与我相关
我的任务
分享
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
void operate_file(char* argv[], int fd, size_t fsize){
size_t buff_size= sizeof(char) * 1024;
//char* buff = static_cast<char*>(malloc(buff_size));
char buff[buff_size];
int i = 0;
ssize_t len;
while((len = read(fd,buff,fsize))>0){
cout<<"len: "<<len<<endl;
cout<<buff<<endl;
cout<<++i<<endl;
}
//free(buff);
;
}
int main(int argc, char *argv[]) {
int fd;
//打开文件
fd = open(argv[1], O_RDWR,S_IRUSR );
if(fd == -1){
cout<<"open err"<<endl;
return -1;
}
size_t size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
//函数指针用于回调
void (*p_operate_file)(char* argv[], int fd, size_t fsize) = &operate_file;
p_operate_file(argv,fd,size);
//关闭文件
close(fd);
}
char buff[buff_size];
当文件较大时,栈溢出了