for a array generally the order will be
A[0][0]
A[0][1]
...
A[0][N]
A[1][0]
A[1][1]
...
A[1][N]
...
A[N][0]
...
A[N][N]
Main Topics
Browse All TopicsI try to read data from a text file into a two-dimensional array in c++.
There are 8 tab seperated column and 5000 rows of integers (shorts).
When I run the code below the b counter is modified to an undetermined value in the loop after a couple (+/- 12-15) runs. When I fix the value to 100 (see below) b changes to 101???
What is wrong with this code? It looks like b is in the same memory as the array or something like that??
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> "You increment 'a' multiple times for 'b'" ??
I am saying that although you have defined the array as ...
short leads[7][4999];
The way you are indexing it means it should be defined thus...
short leads[4999][7];
Look at your nested loops and where you increment 'a' and 'b'
while (getline(myfile, line))
{
iss << line;
while (getline(iss, token, '\t'))
{
leads[a][b] = 100;//(short) atoi(token.c_str());
a++;
}
iss.clear();
b++;
a=0;
}
For every outer iteration you increment 'b' and for every inner iteration you increment 'a' so 'a' is incremented a whole bunch of times for every time 'b' is incremented and you index the array [a][b] but it's defined as [7][5000] so you will end up overrunning the buffer.
>> try define it as leads[5000][8] and try once.
I have already said that http:#25447150
A lot of confusion it seems.
Let me try to shed some light.
The original code from the question only needs a minor change to make it work (assuming the text file has the format you describe : 5000 lines of 8 short values each) :
short leads[7][4999];
has to be changed to :
short leads[8][5000];
This will work, because your indexes in the while loops are set up like that. You use 'a' to count the values on a line, and 'b' to count the lines.
BUT, this is not the expected way of laying out the array. Normally, you'd do it like this :
short leads[5000][8];
so that leads[i][j] refers to the lead on the i-th line, at the j-th position.
This means that you'll have to swap the 'a' and 'b' counters in your code too obviously, so :
leads[b][a] = 100;//(short) atoi(token.c_str());
instead of :
leads[a][b] = 100;//(short) atoi(token.c_str());
with 'b' still the line number, and 'a' still the value index (within the line).
Ok that helps infinity08.
The stupid error was that it should have been short leads[8][5000] instead of leads[7][4999]. I mixed up the fact that in the definition of the array size the actual number of rows should be defined not -1 which is only true for the index (0...7, 0...4999).
Further I understand that normally an array is [5000][8] in this case but I have reasons to do it the other way around because of a assignment later: pRaw->samples[leadNum] = leads[i];
which assigns a whole column by pointer to another structure
>> I mixed up the fact that in the definition of the array size the actual number of rows should be defined not -1 which is only true for the index (0...7, 0...4999).
Precisely :)
>> Further I understand that normally an array is [5000][8] in this case but I have reasons to do it the other way around because of a assignment later
Understood ;)
PjotterR: From the beginning I was trying to say u have to initialyze the array as [5000][8]...u should have tried with [8][5000] atleast!! It took really looong time to solve a simple problem...
I have tried to say the same thing what Infinity08 said...but surely there was "communication gap" not being a C programmer :-)
Business Accounts
Answer for Membership
by: amit2000Posted on 2009-09-29 at 02:16:50ID: 25447090
though I am a Java guy.....your code seems to be strange....
Try this:
while (getline(myfile, line))
{
iss << line;
while (getline(iss, token, '\t'))
{
leads[a][b] = 100;//(short) atoi(token.c_str());
b++;
}
iss.clear();
a++;
b=0;
}