【问题标题】:How to make a char array from a file?如何从文件中创建一个字符数组?
【发布时间】:2015-02-21 04:52:07
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

int count_arr(FILE *file)
{
 int c,count=0;
//FILE *file;
//file = fopen("test.txt", "r");
if (file) {
    while ((c = getc(file)) != EOF){
        putchar(c);
        ++count;}
    fclose(file);
}
return count;
}

void make_arr (FILE *file, char arr[]){
     int c,n=0,count=0;
     char ch;
//FILE *file;
//file = fopen("test.txt", "r");
if (file) {
    while ((c = getc(file)) != EOF){
    ch = (char)c;
    arr[n]=ch;
    ++n; }
    fclose(file);
}

}


int main(){
FILE *file;
int n;
//scanf("%c",&file_name);
file = fopen("test.txt","r");

int count = count_arr(file);
char arr [count];

make_arr(file, arr);

for(n=0; n<count;++n) printf("%c",arr[n]);

}

到目前为止,这就是我的代码的全部内容。我知道我做错了。当我打印出 char 数组时,它会打印出随机垃圾...我正在尝试编写一个函数“make_arr”,该函数传递一个数组,该数组与文件中的字符一起存储。任何帮助,将不胜感激!

【问题讨论】:

  • 可以通过fseek(file, 0, SEEK_END); int size = ftell(file); fseek(file, 0, SEEK_SET);获取文件大小,然后分配内存,读取整个文件char* arr = malloc(size); fread(arr, size, 1, file);。不要忘记关闭文件并释放缓冲区。
  • 代码在 main() 中打开了文件,所以它应该在 main() 中关闭,而不是在两个子函数中。 main() 可以轻松地倒回文件,因此 make_arr() 可以在调用 count_arr() 后立即使用 fseek(file, 0, SEEK_SET) 函数调用再次读取它
  • 使用 fseek(file, 0, SEEK_END); ftell(file) 将在两行代码中给出文件中的字节数,并且比逐字节读取文件要快得多。那么函数 count_arr() 可以被消除
  • 在向全世界展示您的代码之前,请正确格式化/缩进。
  • 如果你没有打开count_arr()中的文件,你不应该关闭它。这样做意味着您在调用count_arr() 后无法使用文件指针。经验法则:在打开文件的函数中关闭文件(仅)。显然,如果函数的目的是打开一个文件并返回打开的文件流,则经验法则不适用。如果您的函数传递了一个打开的文件流,则不应关闭它。如果您的函数自己打开一个文件流,它应该关闭它或返回它(否则会泄漏文件流)。

标签: c arrays file pointers


【解决方案1】:

这是一个将文件读入缓冲区的小例子:

FILE* file = fopen("file.txt", "r");
// get filesize
fseek(file, 0, SEEK_END);
int fsize = ftell(file);
fseek(file, 0, SEEK_SET);
// allocate buffer **note** that if you like
// to use the buffer as a c-string then you must also
// allocate space for the terminating null character
char* buffer = malloc(fsize);
// read the file into buffer
fread(buffer, fsize, 1, file);
// close the file
fclose(file);

 // output data here
for(int i = 0; i < fsize; i++) {
   printf("%c", buffer[i]);
}

// free your buffer
free(buffer);

如果你真的想使用一个函数来填充你的缓冲区,这会起作用(虽然不是真的明白这一点),尽管我仍然只会进行一次读取操作:

void make_array(FILE* file, char* array, int size) {
   // read entire file into array
   fread(array, size, 1, file);
}

int main(int argc,char** argv) {
  // open file and get file size by first
  // moving the filepointer to the end of the file
  // and then using ftell() to tell its position ie the filesize
  // then move the filepointer back to the beginning of the file
  FILE* file = fopen("test.txt", "r");
  fseek(file, 0, SEEK_END);
  int fs = ftell(file);
  fseek(file, 0, SEEK_SET);
  char array[fs];
  // fill array with content from file
  make_array(file, array, fs);
  // close file handle
  fclose(file);

  // output contents of array
  for(int i = 0; i < fs; i++) {
    printf("%c\n", array[i]);
  }
  return 0;
}

就像我在上面的 cmets 中所说,如果您想将 char 数组用作字符串,则需要为终止的空字符添加空格:

char* array = malloc(fs + 1);
fread(array, fs, 1, file);
// add terminating null character
array[fs] = '\0';
// print the string
printf("%s\n", array);

【讨论】:

  • 我如何使用它来将文件存储到数组中?
  • @user3539833 - 两个示例将文件存储到 char 数组中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多