Solved

Dissappearing data in array...

Posted on 2001-06-20
6
204 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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