CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: ifstream

  1. #1
    Join Date
    Apr 2002
    Posts
    17

    Question ifstream

    hi!

    i have a function to read integers from text file and put them respectively in an array. But... the result is not doing that....instead of that it store the last integer from text file as all elements of the array.... i wonder why...can somebody help me with this? Thanks

    void Tau::readFile()
    {
    int arr=0;
    ifstream inClientFile("data3.txt");
    N=ReadNValue();
    int n=readNumVar();

    while (inClientFile>>arr)
    {
    for (int i=0;i<N;++i)
    {
    for (int j=0;j<n;++j)
    {
    Buff[i][j]=arr;
    }
    }
    }
    }

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303
    Hi,

    You are reading a value from the file into 'arr' in the 'while' condition statement, but you then have two 'for' loops inside the while loop. Each time through the while loop, you are writing whatever value you just got as 'arr' into every element of the 2D array.

    Hence when you've read the last element, your 2D array will contain all elements equal to the last value. This clearly isn't your intention.

    Regards
    Alan

  3. #3
    Join Date
    Apr 2002
    Posts
    17
    ok.....thanks....can u pls show me the right way of doing it?

  4. #4
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303
    Personally, I wouldn't bother with a 2D array. You don't show where you allocated Buff, but given a theoretical 2D array X x Y
    Code:
    void Tau::readFile() 
    { 
       ifstream inClientFile("data3.txt"); 
       int arr=0; 
       
       N=ReadNValue(); 
       int n=readNumVar(); 
    
       // array is N rows of n values in dimension
       int count = 0;
         
       while (inClientFile>>arr) 
          Buff[count++]=arr; 
    }
    To access an individual item at row A, column B use Buff[(N*(A-1)) + (B-1)]

    where N * (A-1) = Row A (remember -1)
    B-1 = Bth value (remember -1 again)

    It's probably also worth noting that if your values are in the file in row order (i.e all row one, then row two etc), you can still place them in a 2D array using pointer arithmetic *(Buff+count++) since the memory should be contiguous anyway.

    Regards
    Alan

  5. #5
    Join Date
    Apr 2002
    Posts
    17
    hi again... thanks for ur solution but i'm afraid i don't really get it. I would appreciate it if u can explain it more. Btw... this is where i define the 2D array. Hopefully it's correct

    int Buff[16][5];
    Buff[N][n]=(int)calloc(N,n);

  6. #6
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303
    I'm afraid your declaration of Buff is not correct.
    int Buff[16][5];
    Buff[N][n]=(int)calloc(N,n);
    The first line is ok, in that it declares a 2D array of integers, 16 rows of 5 columns. The second line uses calloc to allocate memory from the heap - this returns a pointer and cannot be assigned to Buff[N][n].

    It's far easier to use a single dimensional array and allocate memory dynamically
    Code:
    int *paBuff = new int[N*n];
    where N and n are as you had them before.

    Then to translate this into a 2D array, consider as an example a 3x5 array (N=3, n=5) simulated as a 1D array of 15 elements. You can imagine the elements laid out in two ways
    [list=1][*]as a 1D array (like paBuff) =

    0 1 2 3 4 5 6 7 8 9 A B C D E
    [*]as a 2D array of N rows of n columns =

    0 1 2 3 4
    5 6 7 8 9
    A B C D E[/list=1]
    To translate from the 1D array into the conceptual 2D array use the formula

    Element at Row R, Column C = (R-1)*(Number of Columns) + (C - 1)

    Hence in the example to get to the 2 row column 3 (i.e 7)

    (2-1)*5 + (3-1) = 5+2 = 7

    i.e given the definition of paBuff this would be paBuff[7]

    You can write this accessor as a class member taking two parameters for row,column if you want to hide the real implementation.

    I hope this helps
    Regards
    Alan
    Last edited by AlanGRutter; May 21st, 2002 at 03:05 AM.

  7. #7
    Join Date
    Apr 2002
    Posts
    17
    ok...i'll try that....thanks!

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    It's far easier to use a single dimensional array and allocate memory dynamically

    Code:
    int *paBuff = new int[N*n];
    where N and n are as you had them before.
    And it is easier still to use std::vector twice:
    Code:
    std::vector< int > row( n, 0 );
    std::vector< std::vector< int > > Buff( N, row );
    An alternative still is to use istream_iterator to read ALL the values into a single std::vector< int >. You can only use this, however, to read the entire stream.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured