簡體   English   中英

如何使用 fread 讀取文件內容?

[英]How can I read with fread the content of file?

給定以下文本文件(“file_text”);

example text

我試圖以這種方式讀取此文件的內容:

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

int main(){
  char* arr = malloc(64);
  FILE* f_fd = fopen("file_txt", "r");
  fread(arr, 1, 64, f_fd);
  printf("|%s|\n", arr);
  return 0;
}

但它不打印文件的真正內容。 例如,我看到: |P?p| |P^2| ..
而且我不明白為什么我明白了。

注意:64 是此文件的最大大小。

您的文件名為“file_text”,但您嘗試使用以下方式打開此文件:

fopen("file_txt", "r");

嘗試用"file_text"文件名打開它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM