我正在尝试使用 Linux 中的 C 编程从资源文件中追加到现有文件。但是,我的代码对此不起作用,any1 可以告诉我代码有什么问题以及 O_APPEND 是如何工作的吗?谢谢 :)
char ifile[150];
char tfile[150];
char cc;
system("clear");
printf("Please enter your resource file name : ");
gets(ifile);
printf("Please enter your destination file name : ");
gets(tfile);
int in, out;
in = open(ifile, O_RDONLY);
int size = lseek(in,0L,SEEK_END);
out = open(tfile, O_WRONLY |O_APPEND);
char block[size];
int pdf;
while(read(in,&block,size) == size)
pdf = write(out,&block,size);
close(in);close(out);
if(pdf != -1)
printf("Successfully copy!");
else
perror("Failed to append! Error : ");
printf("Press enter to exit...");
do
{
cc = getchar();
} while(cc != '\n');