【问题标题】:How do I make the file pointer point at the very start of the file?如何使文件指针指向文件的开头?
【发布时间】:2019-12-09 04:02:19
【问题描述】:

我无法将文件指针设置为文件的开头,以便在已经写入一些文本之后首先写入一些内容。

我尝试过 rewind()、fseek()、以“r+”和“a+”模式打开文件,似乎没有任何效果。

这是该程序的一个小游戏:

#include<stdio.h>
#include<stdlib.h>

void master_globalprint(int lim)
{
    int i = 0;
    FILE* maspass;
    errno_t err;
    err = fopen_s(&maspass, "Master_Password.txt", "r+");
    if (err != 0)
    {
        printf("Error opening Master_Password.txt");
        exit(0);
    }
    rewind(maspass);
    printf("Pointing to %ld", ftell(maspass));
    while (i < lim)
    {
        fprintf(maspass, "%d", i);          //Writing the array infront of the encrypted code
        i++;
    }
    fclose(maspass);
}

void master_create()                                //To Create a Master Password
{
    int count = 0;
    char pass;
    FILE* maspass;
    errno_t err;
    err = fopen_s(&maspass, "Master_Password.txt", "a");
    if (err != 0)
    {
        printf("Error creating Master_Password.txt");
        exit(0);
    }
    printf(" Enter Master Password : ");
    while ((pass = getchar()) != EOF && pass != '\n')
    {
        count++;
        fprintf(maspass, "%c", pass);                       //The characters are then printed one by one
    }
    if (count == 0)
    {
        remove("Master_Password.txt");
        printf("Master Password cannot be empty");
        exit(0);
    }
    fprintf(maspass, "%c", (count + 33));               //To put the amount of letters into file, forwarded by 33 to reach a certain ASCII threshold and converted to char
    fprintf(maspass, "\n");
    fclose(maspass);
    master_globalprint(count);
}

void main()
{
    master_create();
}

上述函数工作并打印正确的值,除了 master_globalprint 函数从最后一个函数停止的位置开始打印。

是因为我必须使用命令行参数来完成任务吗?如果是这样,我是否可以将命令行参数设置为默认执行,这样如果代码被分发,用户就不必费心了?

编辑:在可重现的代码示例中添加。当我在第 31 行输入“​​a”时,它只打印我输入的内容,而不是 master_globalprint() 中的数字。如果我输入“w”,它只会打印 master_globalprint() 中的数字,而不是我输入的内容。

【问题讨论】:

  • "a+" 模式到fopen() 的意思是“所有写入都附加在文件末尾”,无论写入前文件中的当前位置如何。如果您不想附加所有内容,请不要使用a;请改用"r+""w+" 模式。即使您使用"r+""w+" 模式,您也无法在文件中已有的内容之前插入数据;您所能做的就是覆盖已经存在的内容。
  • 即使你使用"r+""w+"模式,你也无法在文件已经存在的内容之前插入数据;您所能做的就是覆盖已经存在的内容。请参阅Inserting data into the middle of a fileDelete data from the middle of a file 了解更多信息。
  • 离题了,但是像fopen_s()这样的函数并不比fopen()这样的标准函数更安全,而且*_s()函数是微软实现的非标准函数且不可携带。
  • printf("error:... (几乎)总是一个错误。应该是fprintf(stderr, "error:...
  • printf("Pointing to %d", ftell(maspass)); //prints 0 every time 是未定义的行为,因为ftell() 返回long int,而不是int。您需要使用%ld

标签: c file-handling


【解决方案1】:

这里的写作模式应该是w+:

err = fopen_s(&maspass, "Master_Password.txt", "a" /* w+ */);

您应该在此处关闭文件,然后将其删除:

if (count == 0)
{
    /*fclose_s(maspass);*/
    remove("Master_Password.txt");
    printf("Master Password cannot be empty");
    exit(0);
}

您应该保持文件描述符打开并将其传递给master_globalprint,而不是这样做:

fclose(maspass);
master_globalprint(count);
/* master_globalprint(count, maspass);
 * fclose(maspass); */

然后继续重用打开的文件描述符。

【讨论】:

  • 已经试过了,不让我在文件的开头写东西。而至少“a+”或“a”写的东西,就在最后,“r+”根本不写任何东西。即使我检查了循环是否在调试中工作。该文件仍然没有得到任何打印。
  • @Chase 您需要在printf 中使用%ld 格式说明符,因为ftell 返回long。更改它并使用 "r+" 重试。
  • 完成,结果相同:/
  • @Chase 你能用fopen试试这个吗?我没有可用的附件 K。
  • 也试过了,结果还是一样。有没有办法通过命令行参数来实现这一点?
猜你喜欢
  • 2015-11-28
  • 2021-11-06
  • 2016-03-05
  • 1970-01-01
  • 2013-09-13
  • 2017-02-05
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多