c - 错误数组大小

标签 c arrays

我需要分配一个包含 300000 个数据的字符数组,这些数据是从文件 Numbers.dat 中获取的,该文件包含以列格式排列的 200,0000 个数字(这是一个巨大的数据文件)。该操作是从该文件中获取数据并将其存储在一个数组中,以 300000 个 block 为单位,以便这 300000 个数字再次存储在不同的文件中。此操作是针对两个文件执行的,因此数字的子集的形式为

-0.98765
-0.124567

等 但是我收到两个错误:第一个是语法错误,说数组大小太长,另一个是逻辑错误。如何解决这个问题。该代码由 Gunner 在 How to read blocks of numbers from a text file in C 中提供,但在用于这种情况时不起作用

#include <stdio.h>
#include<stdlib.h>
# include <conio.h>
# include <dos.h>
# include <math.h>

    void main()
    { FILE *fpt1,*fpt2,*fpt; 
     fp=fopen("numbers.dat","r");
fpt1=fopen("subset1.dat","w");
fpt2=fopen("subset2.dat","w");

int index=0;
char anum[300000]; //this is the reason for the first syntactic error :Array size too large

             // since we are not calculating, we can store numbers as string
            while( fscanf(fp,"%s",anum) == 1 )
            {
                 if(index==0)
                 {
            // select proper output file based on index.
             fprintf(fpt1,"%s",anum);
                 index++; }
                 if(index ==300000)
                 {
                 fprintf(fpt2,"%s",anum);
                 index++; }

             }

fclose(fp);
fclose(fpt1);
fclose(fpt2);
}

逻辑错误是即使我将大小减少到 300 个数据 block ,文件 subset1 和 subset2 中也只写入了一个数字。

最佳答案

您的编译器不支持具有这种容量的静态数组。使用允许这样做的编译器(大多数现代编译器都这样做)。

您也可以尝试动态分配内存。

关于c - 错误数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270618/

相关文章:

c++ - 什么是指针旋转?

c - 为什么这段代码不起作用?

java - 字符串数组 : Adding elements in Loop

javascript - 给定两个相同长度的数组,找到哪些元素是 "shifted"

c - Net-SNMP 自定义 MIB 处理程序

c - 如何使用 sprintf 写入结构中的 char

c 遍历一个数组

c - fopen C 上的段错误

java - 如何使数组大小可变?

java - 遍历数组并检查项目的值