Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 对这种行为有何解释?_C_Linux_File_File Io_Text Files - Fatal编程技术网

C 对这种行为有何解释?

C 对这种行为有何解释?,c,linux,file,file-io,text-files,C,Linux,File,File Io,Text Files,我正在写一个代码来反转一个文本文件。 abc应该成为cba 我把代码写得很好,而且很有效。 但我注意到了这种奇怪的行为。 如果我再次将反向输出作为输入,它将跳过反向输入文件的最后一个字符。 为什么它第一次工作正常,下一次跳过一个角色 代码如下: int main(){ int fin, fout; char c[1]; fin = open("input.txt",O_RDONLY); fout = open("reverse_input.txt",O_WRONL

我正在写一个代码来反转一个文本文件。 abc应该成为cba

我把代码写得很好,而且很有效。 但我注意到了这种奇怪的行为。 如果我再次将反向输出作为输入,它将跳过反向输入文件的最后一个字符。 为什么它第一次工作正常,下一次跳过一个角色

代码如下:

int main(){
    int fin, fout;
    char c[1];
    fin = open("input.txt",O_RDONLY);
    fout = open("reverse_input.txt",O_WRONLY|O_TRUNC);
    lseek(fin, -2, SEEK_END);
    do{
        read(fin, c, 1);
        write(fout,c,1);
    }while(lseek(fin, -2, SEEK_CUR) >=0);
    close(fin);
    close(fout);    
    return 0;
}

我猜我遗漏了某种EOF字符…

您需要将fin中的最后一个字符作为最后一个字符写入fout。

要复制我的结果,请将reverse\u input.txt文件重命名为input.txt,并创建一个空文件reverse\u input.txt。确保它是正确的值。找到它,保存它,然后在关闭文件之前写出来。你写了非常简短的答案,但确实不错,我必须写简短的答案;只有这样我才能打败你!:)