Solved

Dissappearing data in array...

Posted on 2001-06-20
6
197 Views
Last Modified: 2010-04-02
I am using MS Dev C++ 6.0 under Win2k.  The program is a simple console program to split up a data set into 12 frames and will rearrange the data.  The problem is as follows.  Where I know stuff is ok I have summarized.  The problem is that depending where I do the cout statement the data I dumped into the array is printed.  The values are read from a file where some characters have to be skipped.  That is fine.  The output should be values like 1.0013 and so on.

main() ...
...
char *arrIJK[40][54][54];
...
open file...
...
for loops for k < 40 and j < 54 with k++ and j++
  for(int i=0; i < 54; i++)
  {
    fgets(tmp_buffer, 3, r_file);
    fgets(tmp_buffer, 7, r_file);
    arrIJK[k][j][i] = tmp_buffer;
    cout << arrIJK[k][j][i];      // prints ok
    fgets(tmp_buffer, 3, r_file);
    cout << arrIJK[k][j][i];      // prints blank
  }
}
0
Comment
Question by:ancient1
6 Comments
 
LVL 3

Accepted Solution

by:
MDarling earned 300 total points
Comment Utility
Are you using the tmp_bufrer over and over again and expecting the data to persist?

You have a 3 dimensional array of char pointers.

All you seem to be doing is setting each element in turn to point to the same buffer.

Is this what you want?

I'm guessing it's printing blank because it's read blank.

Try this...

cout << "tmp_buffer is " << tmp_buffer << endl
     << "arrIJK[k][j][i] is " << arrIJK[k][j][i] << endl;

and see if you don't get blanks for both.


Fundamentally, though, your use of the pointer array looks flawed.  You need to dynamically allocate a tmp_buffer for each element.


Regards,
Mike.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Please post the comple code for main(), even if you think it's unrelated to the problem.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
>>arrIJK[k][j][i] = tmp_buffer;
error, becouse you change tmp_buffer all time (and
change arrIJK[k][j][i] in parallel.
Right code is:
arrIJK[k][j][i] = strdup(tmp_buffer);
and after end of function you must free data:
free(arrIJK[k][j][i]);

   
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
ancient1, I hope , you use my way to store data.
Alex
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear ancient1

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "MDarling"

comment(s) as an answer and you never provided any feedback.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now