Click to See Complete Forum and Search --> : ifstream
quya
May 20th, 2002, 02:48 AM
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;
}
}
}
}
AlanGRutter
May 20th, 2002, 03:00 AM
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
quya
May 20th, 2002, 03:16 AM
ok.....thanks....can u pls show me the right way of doing it?
AlanGRutter
May 20th, 2002, 03:32 AM
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
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
quya
May 20th, 2002, 09:48 PM
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);
AlanGRutter
May 21st, 2002, 02:57 AM
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
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
quya
May 21st, 2002, 03:12 AM
ok...i'll try that....thanks!
NMTop40
May 23rd, 2002, 05:39 AM
It's far easier to use a single dimensional array and allocate memory dynamically
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:
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.