我应该创建一个程序,该程序采用给定的文件并创建一个带有反向 txt 的文件。我想知道如果我不知道文件的确切大小,是否可以从文件末尾开始 read() 并将其复制到创建文件的第一个字节?我也用谷歌搜索过这个,遇到了很多关于 fread、fopen 等的例子。但是我不能在这个项目中使用这些,我只能使用 read、open、lseek、write 和 close。这是我的代码,到目前为止它并不多,但仅供参考:
#include<stdio.h>
#include<unistd.h>
int main (int argc, char *argv[])
{
if(argc != 2)/*argc should be 2 for correct execution*/
{
printf("usage: %s filename",argv[0[]);}
}
else
{
int file1 = open(argv[1], O_RDWR);
if(file1 == -1){
printf("\nfailed to open file.");
return 1;
}
int reversefile = open(argv[2], O_RDWR | O_CREAT);
int size = lseek(argv[1], 0, SEEK_END);
char *file2[size+1];
int count=size;
int i = 0
while(read(file1, file2[count], 0) != 0)
{
file2[i]=*read(file1, file2[count], 0);
write(reversefile, file2[i], size+1);
count--;
i++;
lseek(argv[2], i, SEEK_SET);
}