如何从文件生成字符数组? #包括 #包括 整数计数(文件*文件) { int c,计数=0; //文件*文件; //file=fopen(“test.txt”、“r”); 如果(文件){ 而((c=getc(文件))!=EOF){ 普查尔(c); ++计数;} fclose(文件); } 返回计数; } void make_arr(文件*文件,字符arr[]){ int c,n=0,count=0; char ch; //文件*文件; //file=fopen(“test.txt”、“r”); 如果(文件){ 而((c=getc(文件))!=EOF){ ch=(char)c; arr[n]=ch; ++n、 } fclose(文件); } } int main(){ 文件*文件; int n; //scanf(“%c”&文件名); file=fopen(“test.txt”、“r”); int count=count\u arr(文件); 字符arr[计数]; 制作arr(文件,arr); 对于(n=0;n

如何从文件生成字符数组? #包括 #包括 整数计数(文件*文件) { int c,计数=0; //文件*文件; //file=fopen(“test.txt”、“r”); 如果(文件){ 而((c=getc(文件))!=EOF){ 普查尔(c); ++计数;} fclose(文件); } 返回计数; } void make_arr(文件*文件,字符arr[]){ int c,n=0,count=0; char ch; //文件*文件; //file=fopen(“test.txt”、“r”); 如果(文件){ 而((c=getc(文件))!=EOF){ ch=(char)c; arr[n]=ch; ++n、 } fclose(文件); } } int main(){ 文件*文件; int n; //scanf(“%c”&文件名); file=fopen(“test.txt”、“r”); int count=count\u arr(文件); 字符arr[计数]; 制作arr(文件,arr); 对于(n=0;n,c,arrays,file,pointers,C,Arrays,File,Pointers,,这里有一个将文件读入缓冲区的小示例: #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;}

,这里有一个将文件读入缓冲区的小示例:

#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]);

}

您可以通过
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_END);ftell(file)再次使用fseek(file,0,SEEK_SET)函数调用读取它将以两行代码给出文件中的字节数,这比逐字节读取文件要快得多。那么函数count_arr()可能会被删除。请在向世界展示之前正确格式化/缩进代码。如果您没有在
count_arr()中打开文件
,您不应该关闭它。这样做意味着您在调用
count\u arr()
后不能使用文件指针。经验法则:在打开文件的函数中关闭文件(仅限)。显然,如果函数的目的是打开文件并返回打开的文件流,则经验法则不适用。如果向函数传递打开的文件流,则不应将其关闭。如果函数本身打开文件流,则应将其关闭或返回(否则会泄漏文件流)。我如何使用它将文件存储到数组中?@user3539833-两个示例都将文件存储到char数组中。
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;
}
char* array = malloc(fs + 1);
fread(array, fs, 1, file);
// add terminating null character
array[fs] = '\0';
// print the string
printf("%s\n", array);