用 C 覆盖文件中的行

发布于 2024-08-26 09:25:26 字数 731 浏览 5 评论 0原文

我正在大学操作系统课程中做一个关于文件系统的项目,我的C程序应该在人类可读的文件中模拟一个简单的文件系统,所以文件应该基于行,一行将是一个“扇区”。我了解到,要覆盖的行必须具有相同的长度,因此我将用 ASCII 零填充它们,直到行尾,并留下一定数量的 ASCII 零行,以便稍后填充。

现在我正在制作一个测试程序,看看它是否像我想要的那样工作,但事实并非如此。我的代码的关键部分:

file = fopen("irasproba_tesztfajl.txt", "r+"); //it is previously loaded with 10 copies of the line I'll print later in reverse order  

  /* this finds the 3rd line */
 int count = 0; //how much have we gone yet?
 char c;

 while(count != 2) {
  if((c = fgetc(file)) == '\n') count++;
 }

 fflush(file);

 fprintf(file, "- . , M N B V C X Y Í Ű Á É L K J H G F D S A Ú Ő P O I U Z T R E W Q Ó Ü Ö 9 8 7 6 5 4 3 2 1 0\n");

 fflush(file);

 fclose(file);

现在它什么也不做,文件保持不变。可能是什么问题?

谢谢。

I'm doing a project on filesystems on a university operating systems course, my C program should simulate a simple filesystem in a human-readable file, so the file should be based on lines, a line will be a "sector". I've learned, that lines must be of the same length to be overwritten, so I'll pad them with ascii zeroes till the end of the line and leave a certain amount of lines of ascii zeroes that can be filled later.

Now I'm making a test program to see if it works like I want it to, but it doesnt. The critical part of my code:

file = fopen("irasproba_tesztfajl.txt", "r+"); //it is previously loaded with 10 copies of the line I'll print later in reverse order  

  /* this finds the 3rd line */
 int count = 0; //how much have we gone yet?
 char c;

 while(count != 2) {
  if((c = fgetc(file)) == '\n') count++;
 }

 fflush(file);

 fprintf(file, "- . , M N B V C X Y Í Ű Á É L K J H G F D S A Ú Ő P O I U Z T R E W Q Ó Ü Ö 9 8 7 6 5 4 3 2 1 0\n");

 fflush(file);

 fclose(file);

Now it does nothing, the file stays the same. What could be the problem?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-09-02 09:25:26

此处

当使用“+”打开文件时
选项,您可以读取和写入
它。但是,您可能不执行
输出操作后立即
输入操作;你必须执行
介入“倒带”或“fseek”。
同样,您也不能执行
紧接着输入操作
输出操作;你必须执行
介入“倒回”或“fseek”。

因此,您已经使用 fflush 实现了这一目标,但为了写入所需的位置,您需要 fseek 返回。这就是我实现它的方式 - 我想可能会更好:

 /* this finds the 3rd line */
 int count = 0; //how much have we gone yet?
 char c;
 int position_in_file;

 while(count != 2) {
  if((c = fgetc(file)) == '\n') count++;
 }

 // Store the position
 position_in_file = ftell(file);
 // Reposition it
 fseek(file,position_in_file,SEEK_SET); // Or fseek(file,ftell(file),SEEK_SET);

 fprintf(file, "- . , M N B V C X Y Í Ű Á É L K J H G F D S A Ú Ő P O I U Z T R E W Q Ó Ü Ö 9 8 7 6 5 4 3 2 1 0\n");  
 fclose(file);

另外,正如所评论的那样,您应该检查您的文件是否已成功打开,即在读取/写入文件之前,检查:

file = fopen("irasproba_tesztfajl.txt", "r+");
if(file == NULL)
{
  printf("Unable to open file!");
  exit(1);
}

From here,

When a file is opened with a "+"
option, you may both read and write on
it. However, you may not perform an
output operation immediately after an
input operation; you must perform an
intervening "rewind" or "fseek".
Similarly, you may not perform an
input operation immediately after an
output operation; you must perform an
intervening "rewind" or "fseek".

So you've achieved that with fflush, but in order to write to the desired location you need to fseek back. This is how I implemented it - could be better I guess:

 /* this finds the 3rd line */
 int count = 0; //how much have we gone yet?
 char c;
 int position_in_file;

 while(count != 2) {
  if((c = fgetc(file)) == '\n') count++;
 }

 // Store the position
 position_in_file = ftell(file);
 // Reposition it
 fseek(file,position_in_file,SEEK_SET); // Or fseek(file,ftell(file),SEEK_SET);

 fprintf(file, "- . , M N B V C X Y Í Ű Á É L K J H G F D S A Ú Ő P O I U Z T R E W Q Ó Ü Ö 9 8 7 6 5 4 3 2 1 0\n");  
 fclose(file);

Also, as has been commented, you should check if your file has been opened successfully, i.e. before reading/writing to file, check:

file = fopen("irasproba_tesztfajl.txt", "r+");
if(file == NULL)
{
  printf("Unable to open file!");
  exit(1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文