FILE I/O problem please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mayankladoia
    New Member
    • Jul 2010
    • 2

    FILE I/O problem please help

    Following is the code which i am running on arr[2000][2000] array:

    ifstream inf;
    inf.open("ABC1. txt");

    for(i=0;i<x;i++ )
    {
    for(j=0;j<x;j++ )
    {
    inf>>arr[i][j];
    if(arr[i][j]==0)
    {
    ind[i][j]=0;
    }
    else
    {
    ind[i][j]=1;
    }
    }
    }

    after running this code arr[][] contains only 0 in all cells ie arr[][]= 0 is
    0 0 0 0 0 0 0 0 ............ 2000 times
    0 . . . ............... .....
    0 . . . ............... .....
    0 ............... ..........
    0 ............... .........
    .
    .
    .
    2000 times

    This only happens if size of x is 2000 for x=1000 same code is running fine.

    Can you suggest solution for the same.....
    Thanks in advance
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I suspect your array is too large for the stack.

    A 2000x2000 array is 4 million elements.

    If the array is char that's a 4MB stack array. If the array is int ons 32 bit machine that's a 16MB array.

    A big stack frame is 8K.

    You should be able to allocate the array on the heap and it should work OK at 2000x2000.

    Comment

    • mayankladoia
      New Member
      • Jul 2010
      • 2

      #3
      This is the beginning of the code ... It is like an input text file is given which is an array of n X n i.e a 2D array value of n is 2000. This text file is opened by following lines

      ifstream inf;
      inf.open("ABC1. txt");

      And then the above given for loop runs on this code. when i am trying to display arr array by adding this line after inf>>arr[i][j] in the above code
      cout<<arr[i][j]<<"\n";

      It is only displaying 0 for more than 2000 elements which clearly indicates that it is unable to read data from text file for large input
      I dont know how to use heap for this please tell me in case u have any idea

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Allocate on the heap by:

        Code:
        int (*arr)[2000] = new int[2000][2000];
        assuming your array is an array of int.

        The code aboive is exactly the same as:

        Code:
        int arr[2000][2000];
        With the heap array ytou need to delete it when you are done with it:

        Code:
        delete [] arr;
        See if this makes a difference. Be sure to post again and tell me what happened.

        BTW: There is a C/C++ Insight paper called Arrays Revealed that you might read.

        Comment

        Working...