Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为数组动态分配内存并读取大型文本文件_Arrays_C_Memory_Dynamic - Fatal编程技术网

Arrays 为数组动态分配内存并读取大型文本文件

Arrays 为数组动态分配内存并读取大型文本文件,arrays,c,memory,dynamic,Arrays,C,Memory,Dynamic,我已经看了一些其他类似的问题和例子,但我被难住了。我的目标是打开一个非常大的文本文件(新大小),将内存分配给一个数组,然后将文本存储到该数组中,以便将来能够进行进一步的处理 这是我当前的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #define LINELEN 74 int main(void) { FILE *file; char filename[] = "l

我已经看了一些其他类似的问题和例子,但我被难住了。我的目标是打开一个非常大的文本文件(新大小),将内存分配给一个数组,然后将文本存储到该数组中,以便将来能够进行进一步的处理

这是我当前的代码:

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

#define LINELEN 74

int main(void) {

FILE *file;
char filename[] = "large.txt";
int count = 0, i = 0, len;

/* Open the file */
  file = fopen(filename, "r");
  if (file == NULL) {
      printf("Cannot open file");
      return -1;
  }
    
/* Get size of file for memory allocation */
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
/* Allocate memory to the array */
  char *text_array = (char*)malloc(size*sizeof(char));
    
/* Store the information into the array */
    while(fgets(&text_array[count], LINELEN, file) != NULL) {
      count++;
      }

  len = sizeof(text_array) / sizeof(text_array[0]);

  while(i<len) {
    /* printf("%s", text_array); */
    i++;
  }
  printf("%s", text_array);

/* return array */
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#定义LINELEN 74
内部主(空){
文件*文件;
char filename[]=“large.txt”;
整数计数=0,i=0,len;
/*打开文件*/
file=fopen(文件名为“r”);
if(file==NULL){
printf(“无法打开文件”);
返回-1;
}
/*获取用于内存分配的文件大小*/
fseek(文件,0,SEEK_END);
长尺寸=ftell(文件);
fseek(文件,0,搜索集);
/*为阵列分配内存*/
char*text_数组=(char*)malloc(size*sizeof(char));
/*将信息存储到数组中*/
while(fgets(&text_数组[count],LINELEN,file)!=NULL){
计数++;
}
len=sizeof(文本数组)/sizeof(文本数组[0]);
而(i

while(fgets(&text_array[count], LINELEN, file) != NULL) {
  count++;
}
这是有问题的

如果循环未滚动,则“有点像”:

因此,您只需在每个
fgets
调用之间将
fgets
目标缓冲区提前一个字符。如果假定
fgets
读取的字符多于一个,则第二个
fgets
将覆盖第一个
fgets
中的数据。第三个
fgets
将覆盖第二个
中的数据,依此类推


您需要使用与实际读取或使用另一种读取方式(例如,
fread
)一样多的字符推进缓冲区,无需在循环中调用
fgets()
。您知道文件有多大,只需一次调用即可将整个内容读入
text\u数组

fread(text_array, 1, size, file);
但是,如果要将
text\u数组
视为字符串,则需要添加空终止符。因此,在调用
malloc()
时应添加1

另一个问题是
len=sizeof(text\u array)/sizeof(text\u array[0])
text\u array
是一个指针,而不是数组,因此不能使用
sizeof
来获取它所使用的空间量。但不需要这样做,因为
size
变量中已经有了空间

无需在循环中打印
文本数组

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

#define LINELEN 74

int main(void) {

    FILE *file;
    char filename[] = "large.txt";
    int count = 0, i = 0, len;

/* Open the file */
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Cannot open file");
        return -1;
    }
    
/* Get size of file for memory allocation */
    fseek(file, 0, SEEK_END);
    size_t size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
/* Allocate memory to the array */
    char *text_array = (char*)malloc(size*sizeof(char) + 1);
    
/* Store the information into the array */
    fread(text_array, 1, size, file);
    text_array[size] = '\0';
    printf("%s, text_array);

/* return array */
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#定义LINELEN 74
内部主(空){
文件*文件;
char filename[]=“large.txt”;
整数计数=0,i=0,len;
/*打开文件*/
file=fopen(文件名为“r”);
if(file==NULL){
printf(“无法打开文件”);
返回-1;
}
/*获取用于内存分配的文件大小*/
fseek(文件,0,SEEK_END);
大小\u t size=ftell(文件);
fseek(文件,0,搜索集);
/*为阵列分配内存*/
char*text_数组=(char*)malloc(size*sizeof(char)+1);
/*将信息存储到数组中*/
fread(文本数组,1,大小,文件);
文本数组[大小]='\0';
printf(“%s,文本数组”);
/*返回数组*/
返回退出成功;
}

您试图使用
文本数组
就像它是一个二维数组一样。为什么要使用
fgets()
?只需调用
fread()即可读取整个文件
。如果要访问存储文件中的每一行,另一个选项是为每一行动态分配一个指针,然后分配内存来保存由
fgets()
读取的行(不要忘记删除
'\n'
并为nul终止字符添加
+1
),将该行复制到已分配的块中,并将块的起始地址分配给指针。当需要更多指针时,
realloc
并继续。例如,
char**lines;
然后分配指针,
size\t p\u avail=2,p\u used=0;line=malloc(p\u avail*sizeof*lines)
Realloc when
p\u used==p\u avail
。然而,如果您只想将整个文件存储在一个数组中,那么@Barmar显示的
fread
是最好的方法。(还有一个技巧,分配
size+1
字节并设置
text\u数组[size]=0;
您可以将整个内容视为字符串)很快——你就成了一只企鹅了……嘿,各位,谢谢你们的帮助!对不起,我花了这么长时间才回答。回答几个问题,这是一个作业,我被要求使用fgets。很遗憾,大卫·C·兰金,我也要尝试一下你们的解决方案,它似乎达到了我对fgets的要求。最后,“大体上”是一个6.4MB的文本文件不是很大,但是需要处理大量的文本。非常感谢!这实际上澄清了所有我不了解的事情。非常感谢。我应该提到我需要使用fgets,但你给我的确实澄清了我的很多问题。谢谢。因为每一个行覆盖了最后的大部分,这就是为什么我得到的是一系列随机的不连贯字符,而不是一个文本体?你说“如果我们假设fgets读取多个字符”。如果是这样,我正在覆盖字符,我如何使用fgets获取整个文本体?我已经尝试将其更改为:while(fgets(&text\u数组)[count],1,file)!=NULL)认为它会一次读取一个字符并将其输入到数组中,但是我得到了一个分段错误,但是内存I malloc应该足够了,不是吗?谢谢你的回答。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINELEN 74

int main(void) {

    FILE *file;
    char filename[] = "large.txt";
    int count = 0, i = 0, len;

/* Open the file */
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Cannot open file");
        return -1;
    }
    
/* Get size of file for memory allocation */
    fseek(file, 0, SEEK_END);
    size_t size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
/* Allocate memory to the array */
    char *text_array = (char*)malloc(size*sizeof(char) + 1);
    
/* Store the information into the array */
    fread(text_array, 1, size, file);
    text_array[size] = '\0';
    printf("%s, text_array);

/* return array */
    return EXIT_SUCCESS;
}