开发者

Error array size

开发者 https://www.devze.com 2023-02-16 00:20 出处:网络
I need to assign a character array containing 300000 data which are fetched from a fileNumbers.dat containing 200,0000 numbers arranged in a column format(its a huge data file). The operation is to fe

I need to assign a character array containing 300000 data which are fetched from a file Numbers.dat containing 200,0000 numbers arranged in a column format(its a huge data file). The operation is to fetch in data from this file and store it in an array in blocks of 300000 so that these 300000 numbers are again stored in different files.This operation is performed for two files which are therefore subsets of the Numbers are of form

-0.98765
-0.124567

etc But I am getting TWo errors : First is syntactic error saying the array size is too long and the other is logical error. How to resolve this. The code is as under provided by Gunner in How to read blocks of numbers from a text file in Cbut not working when used for this case

#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,开发者_StackOverflow"%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);
}

The logical error is that only one number is being written in file subset1 and subset2 even when I reduce the size to 300 blocks of data.


Your compiler doesn't support static arrays with such a capacity. Use a compiler that allows this(most modern compilers do).

You can also try to allocate the memory dynamically.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号