0

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,"%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.

7
  • 2
    Which source? Which compiler? Which command line? Which error message? How to ask questions the smart way: catb.org/~esr/faqs/smart-questions.html Commented Mar 11, 2011 at 8:36
  • @To all: I have TURBO C++ FOR WINDOWS 4.5. Commented Mar 11, 2011 at 8:50
  • @Gunner: This is actally the code for my other question asked in my other question just answered by you regarding the file read operation in blocks Commented Mar 11, 2011 at 8:51
  • 1
    I also use Turbo C (in several flavors) just for nostalgia purposes, but we need to see the code and the error. I'd recommend updating your question quickly. If it is closed, please still consider updating it and flagging it to be re-opened. Commented Mar 11, 2011 at 8:54
  • @Tim Post: Code added please resolve the issue Commented Mar 11, 2011 at 9:15

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@Gunner: The array size is required for the Question which you answered where the sample was with a character array of size 100. In the real case each file should conatin 2359296 data(numbers). So I think you have followed the error and the problem. :)
@SKM: do you understand what dynamic memory is? Gunner's suggesting something like "int* p = malloc(300000 * sizeof(int));". It should definitely work, even with that old compiler ;-P.
@Gunner and Tim : Is there no other option apart from dynamicmemory allocation? I have modified the Question with the code
@SKM - You just can't do what you are asking. You could try multiple static pools and allocate from that, but .. ewww! What prevents you from using a modern C library and compiler?
@Then kindly suggest a modern C library and a compiler.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.