gpt4 book ai didi

c - 错误数组大小

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:07 25 4
gpt4 key购买 nike

我需要分配一个包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com