简体   繁体   English

使用fread从C中的文件读取

[英]Reading from file in C using fread

I'm learning how to read content from a file in C. And I manage to scrape through the following code. 我正在学习如何从C语言的文件中读取内容。并且设法抓取以下代码。

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


void read_content(FILE *file) {
  char *x = malloc(20);
  // read first 20 char
  int read = fread(x,sizeof(char),20,file);
  if (read != 20) {
     printf("Read could not happen\n");
   }
  else {
      printf("the content read is %s",x);
  }
  free(x);
  return; 
}

int main(int argc,char *argv[]) {

  FILE *fp; 
  fp = fopen("test.txt","w+");
  read_content(fp);
  fclose(fp);
  return 0;
}

But for some reason (which I'm not able to understand) I see the read bytes count as 0 . 但是由于某种原因(我无法理解),我将read字节数视为0

The problem is that you open the file with the w+ mode. 问题是您使用w+模式打开文件。 There are two possibilities: 有两种可能性:

  • if the file doesn't exist, it will be created empty. 如果文件不存在,它将被创建为空。 Reading from it immediately gives end of file resulting in fread() returning 0. 立即从中读取将给出文件结尾,导致fread()返回0。
  • if the file does exist, it will be truncated ie changed into an empty file. 如果文件确实存在,它将被截断,即更改为空文件。 Reading from it immediately gives end of file resulting in fread() returning 0. 立即从中读取将给出文件结尾,导致fread()返回0。

If you just want to read from the file (as per your example), open it in mode r . 如果只想读取文件(按照您的示例),请在模式r打开它。 If you want to read and write without destroying its existing content, use mode r+ . 如果您想在不破坏其现有内容的情况下进行读写,请使用模式r+

Whatever mode you choose, always check that fopen() returns non null and print the error if it returns null (this is not the cause of your problem but is best practice). 无论选择哪种模式,请始终检查fopen()返回非null,如果返回null则显示错误(这不是问题的原因,而是最佳实践)。

From Man Page w+ flag: 从手册 w+标志:

Open for reading and writing. 开放供阅读和写作。 The file is created if it does not exist, otherwise it is truncated. 如果该文件不存在,则创建该文件,否则该文件将被截断。

You are probably trying to open a file which doesn't exist at the path you provided, or is read-only as @WhozCraig suggested in comment. 您可能正在尝试打开您提供的路径中不存在的文件,或者如@WhozCraig在注释中建议的那样为read-only This means a new file is being created, an empty file! 这意味着正在创建一个新文件,一个空文件! hence you are seeing 0 bytes read. 因此,您看到0字节的读取。

To sum up, The fopen is failing, in that case you need to check the return value if it is equal to -1 . 综上所述, fopen失败,在这种情况下,您需要检查返回值是否等于-1
To find what was the error, you can check the errno as it is set to indicate the error. 要了解什么是错误,你可以检查errno ,因为它被设置为显示错误。

If you are only intending to read, open the file with r flag instead of w+ 如果只打算读取,请使用r标志而不是w+打开文件

The problem lies within this line of code: 问题在于以下代码行:

fp = fopen("test.txt","w+") 

the "w+" mode, clear the previous content of the file and the file will be empty when you just going to read the file without writing anything to it. "w+"模式下,清除文件的先前内容,并且仅在不写入任何内容的情况下读取文件时,文件将为空。 Hence, it is printing "Read could not happen" because you are trying to read an empty file. 因此,它正在打印"Read could not happen"因为您正在尝试读取一个空文件。

I would suggest you to use "r+" mode, if you are willing to read and then write into the file. 如果您愿意读取然后写入文件,我建议您使用"r+"模式。 Otherwise, r mode is good enough for simple reading of a file. 否则, r模式足以简单读取文件。

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

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