该程序想要从文件读取。该文件中的内容是字符串“Hello,world”。然后判断字符串的每个字符,以查看字符是否大于或等于常量字符'e',如果字符符合条件,则按字母顺序将字符更改为其前一个字符(例如,'b'更改到'一','E'改为'D')。最后,将更改的文件内容输出到屏幕上。同时在一个流中使用fread和fwrite导致问题
问题是,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");
}
您最初将20个字节写入文件,但's'变量仅用13个字符进行初始化。其他7个垃圾 - 应该考虑的东西。由于书写也提高了文件的位置,所以你需要存储pos2的标记(但是你可以完成'fseek(fp,pos1,SEEK_SET)'而不是'fseek(fp,pos2,SEEK_SET)') – nos
是的,你是对。我只希望人们可以轻松阅读代码。变量pos1的使用是显示文件位置。从文件读取后,pos2用于存储文件的位置,以便我可以返回到该位置。结果证明我认为在从文件中读取时可能有一个指针来推进文件,并且在写入文件时有另一个指针来推进它。我没有经验,我静态分配了字符串的长度来存储文件的内容。 – toolchainX
@Vicky感谢您的格式化。既然这是我第一次使用这个网站,我会在这里做的更好。 – toolchainX