Solved

Dynamic Data Storage

Posted on 2000-04-12
4
178 Views
Last Modified: 2010-04-15
I have a program that will read a complete line of data from a text-file into a data structure (MANY vars in the structure).

The data file currently holds 8000 lines of data. I was at first tempted to define an arry of the structures that i have.....ie...
_MyTypedefedStruc MyPtrsToTheTypedefedStructure[8000];

But, i have decided that the data file may increase in size later on (more data added, thus, more structures would have to be added to the static array, so that they could be read in later.)

But i dont wanna do it this way! I dont want to have to recompile and distribute the exe each time this happens!!!

My question is this (obvious now):

How can i dynamicly define these structures?

I have looked at link-lists, and it seems to be the best way currently, but i would prefer not to use this method (lack of information and i dont like all the extra crap required for them).

The person that leads me to information that will help get me on track so i can dynamicly allocate structures will get the points.

They will be handed to a person tht gives full source (commented!) on linked lists too. But, as i want to avoid the use of link-lists, the points WILL be denied if no one else has taken a bash at this question.

BTW I will use C/C++ or for this project, i will NOT use MFC, dont bother with mfc replys.

Im working with Visual C version 6 and this program will use directx under the windows 98 environment.

Thanks people, i know you'll help this poor old hobby programmer.

Chris.
0
Comment
Question by:ccheek
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 150 total points
ID: 2706986
2 pass system


First read the file in but don't store the data just count the number of elements

rewind the file

_MyTypedefedStruc *pdata;

pdata = calloc (countedelements,sizeof (_MyTypedefedStruc));

if (pdata == NULL) {
/* Error occured */
 } else {
  read in the data and store id in pdata[]

}
0
 
LVL 10

Expert Comment

by:rbr
ID: 2707009
Or a demo source code


datafile.txt


one
two
three


readin.c

#include <stdio.h>
#include <stdlib.h>

struct my_struct {
char text[100];
}
int main (int argn,char argv[])
{
     FILE *fp;
     struct my_struct *pdata;
     char buffer[100];
     int i,count=0;

      fp=fopen ("datafile.txt","r");

      while (EOF=fscanf (fp,"&s",buffer)) {
          count++;
     }
      rewind (fp);
     pdata = calloc (count,sizeof (struct my_struct)) {
      if (pdata != NULL)
         for (i=0;i<count;i++) {
             fscanf (fp,"&s",pdata[i].text);
        }
     }
     fclose (fp);
      return 0;
}
   
0
 
LVL 10

Expert Comment

by:rbr
ID: 2707014
Sorry !

fscanf (fp,"&s",buffer)
should be
fscanf (fp,"%s",buffer)

fscanf (fp,"&s",pdata[i].text);
should be
fscanf (fp,"%s",pdata[i].text);
0
 

Author Comment

by:ccheek
ID: 2708862
I have to admit i didnt know calloc would allow this!

I did in fact look at it a while back and thought "Naahhhh surely not!" (and didnt bother coding a sample app.)

Thankyou for a quick, and simple answer rbr.

I can now continue my project, i cant believe something so stooopid stumped me!! :)

Chris.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

762 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

22 Experts available now in Live!

Get 1:1 Help Now