gpt4 book ai didi

c - 同时在一个流中使用 fread 和 fwrite 会导致问题

转载 作者:行者123 更新时间:2023-11-30 18:04:49 25 4
gpt4 key购买 nike

该程序想要从文件中读取。文件中的内容是字符串“Hello, world”。然后判断字符串的每个字符,看该字符是否大于等于const字符'e',如果该字符满足条件,则将该字符更改为按字母顺序排列的前一个字符(例如'b'更改为为“a”,“e”更改为“d”)。最后将更改后的文件内容输出到屏幕上。

问题是 fwrite 和 fread 是如何工作的?为什么我不能去掉变量 pos2 来简化表达式。如果有人可以帮忙,非常感谢!

#include <stdio.h>

int main()

{
FILE *fp;
char s[20];
char t[20];
char transfer;
int i;
int pos; // storing the position of the file before reading from the file
int pos1; // check the position of the file
int pos2; // storing the position of the file after reading from the file
#pragma region create a file named "Hello", write "Hello, world" into the file, close it
if ((fp = fopen("Hello", "wb") )== NULL)
{
printf("can't open file\n");
exit(0);
}
strcpy(s, "Hello, world");
fwrite(s, sizeof(char)*20, 1, fp);
fseek(fp, 0, SEEK_SET);
fclose(fp);
#pragma endregion create a file named "Hello", write "Hello, world" into the file, close it
#pragma region read from the file named "Hello", deal with its current, write the change into the file.
if ((fp = fopen("Hello", "rb+")) == NULL )
{
printf("can't open file\n");
exit(1);
}
i = 0;
while(i < 20)
{
// 提问,该处为何不能利用fwrite的自动定位免去注释掉的语句行(即使用了pos2的语句行)。
// Here is the problem. since the fread and fwrite function can move the position of the
// file, I think I can get rid off the following commented two sentences with the
// variable pos2 in it
pos = ftell(fp); // storing the position before reading from file
fread(&transfer, sizeof(char), 1, fp); // the position of the file moved to the next char
// pos2 = ftell(fp); // storing the position after reading from file
pos1 = ftell(fp);
if (transfer >= 'e') // if the character greater or equal to 'e' minus 1.
{
transfer -= 1;
}
fseek(fp, pos, SEEK_SET); // back to the position where the character is read to change the char
fwrite(&transfer, sizeof(char), 1, fp);// the position of the file moved to the next char
// fseek(fp, pos2, SEEK_SET); //
pos1 = ftell(fp);
i++;
}
fseek(fp, 0, SEEK_SET);
fclose(fp);
#pragma endregion read from the file named "Hello", deal with its current, write the change into the file.
#pragma region read from the file named "Hello", output the changed string
if((fp = fopen("Hello", "rb")) == NULL)
{
printf("Can't open file\n");
exit(2);
}
fread(t, sizeof(char)*20, 1, fp);
printf("The output is: %s \n", t);
// the right output is (the two sentences above with pos2 in it is commented) :
// The output is: Hdkkn,vnqkd
// the wrong output is (the two sentences above with pos2 in it isn't commented):
// The output is: Hddddddddddddddddddd烫烫烫烫Hello, world
fseek(fp, 0, SEEK_SET);
fclose(fp);
#pragma endregion read from the file named "Hello", output the changed string
system("pause");
}

最佳答案

我实际上不明白为什么你要尝试注释/注释这两行。因为无论您注释它们还是注释它们,都不会改变。您已经在代码中删除了 pos2 (这就是您所要求的)。

因此,如果您在 while 循环中使用以下代码

pos = ftell(fp);     // storing the position before reading from file
fread(&transfer, sizeof(char), 1, fp);
pos1 = ftell(fp);
if (transfer >= 'e') // if the character greater or equal to 'e' minus 1.
{
transfer -= 1;
}
fseek(fp, pos, SEEK_SET);
fwrite(&transfer, sizeof(char), 1, fp);
i++;

然后你会得到“输出是:Hdkkn,vnqkd”,这是预期的结果。

您还可以将文件中的每一行放入数组并对其进行操作,然后将其写回文件。通过这种方式,它可以更通用,并且您不必使用像“20”这样的神奇数字。

编辑:

我在 Linux 平台上使用 gcc 4.5.2。我不想对其他平台发表评论,但正如我之前提到的,您可以将该行放入缓冲区,然后在操作后可以将其写回。您可以尝试用 while 循环替换以下代码:

char line[20] = {0};

fread(line, sizeof(char), 20, fp);

for(i = 0; i < strlen(line); i++)
{
if(line[i] >= 'e')
line[i] -= 1;
}

fseek(fp, 0, SEEK_SET);
fwrite(line, sizeof(char), strlen(line), fp);

通过这种方式你可以摆脱许多变量。这是你的选择。

关于c - 同时在一个流中使用 fread 和 fwrite 会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com