Solved

Dissappearing data in array...

Posted on 2001-06-20
6
201 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
ID: 6209734
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
ID: 6210055
Please post the comple code for main(), even if you think it's unrelated to the problem.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6210226
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

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

Expert Comment

by:griessh
ID: 7014548
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
ID: 7030983
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 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

20 Experts available now in Live!

Get 1:1 Help Now