Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dissappearing data in array...

Posted on 2001-06-20
6
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

by:
MDarling earned 1200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

721 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