简体   繁体   English

文件程序-fseek不起作用

[英]File program - fseek isnt working

I'm having trouble making this fseek() function work in my code. 我在使此fseek()函数在我的代码中工作时遇到麻烦。 The text I wrote just doesn't start at the point I indicate and I don't know why. 我写的文字并不是从我指出的那一点开始的,我也不知道为什么。 It should start writing from the \\n and it just overwrite all the text file. 它应该从\\ n开始写入,并且只会覆盖所有文本文件。 Even if I open it with a it just doesn't go where I command through the parameters. 即使我用a打开它,也不会在我通过参数命令的地方进行。

   fclose(file);
    FILE *file_a = fopen("ex6.txt", "w");

    fseek(file_a, -1, SEEK_END);

    puts("Write text to add:");
    while((letter = getchar()) != '\n')
    {
        fputc(letter, file_a);
    };

What is happening? 怎么了? Why doesnt this work? 为什么不起作用?

Navigating to absolute only works when the file is opened in binary mode. 导航到绝对值仅在以二进制模式打开文件时有效。 When it's opened in text mode, fseek() cannot navigate to absolute positions in a file besides 0 (the start of the file), and trying to do so will result in undefined behaviour. 当它以文本模式打开时, fseek()无法导航到文件中除0(文件的开头)之外的绝对位置,而尝试这样做将导致不确定的行为。 You can, however, navigate to references in the file returned by ftell() . 但是,您可以导航到ftell()返回的文件中的引用。 The reason for this is due to the handling of certain characters by some operating systems; 原因是由于某些操作系统对某些字符的处理。 some implementations allow it but POSIX doesn't mandate it. 一些实现允许它,但是POSIX并不要求它。

I know you solved the issue in the comments, this is just for closure. 我知道您已在评论中解决了该问题,这仅是为了解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM