Solved

dynamic memory buffer

Posted on 2002-03-12
8
241 Views
Last Modified: 2010-04-01
I need to read some data from a binary file into a memory buffer. I cannot use array of chars because I have limited memory and i don't know how long the array is going to be. Should I use a CString, cArrays, vectors or what? some example would be appreciated.

Thanks in advance
0
Comment
Question by:el_rooky
[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
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6857734
>>I have limited memory and i don't know how long the
>>array is going to be

Read the file's length beforehand:

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
   struct _stat buf;
   int result;
   char* pBuf;

   /* Get data associated with "stat.c": */
   result = _stat( "file.bin", &buf );

   /* Check if statistics are valid: */
   if( result != 0 )
      perror( "Problem getting information" );
   else
   {
      pBuf = new char [ buf.st_size];

      // read bin file
   }
}

0
 

Author Comment

by:el_rooky
ID: 6857899
I failed to mention that this application is for a winCE device and memory is very limited. I dont want to allocate memory for the entire file size since this file is large and I only need to read portions of the file.

I also should mention that this file contains a file
header with and index to 512 blocks of the data which are not necessarily sequential. After reading all this blocks of data I need to place them in an array of objects.

but if I dont have any other choice, I guess I could use a char* and estimate the lenght of the buffer.
thanks
still el_rooky
0
 
LVL 86

Expert Comment

by:jkr
ID: 6857924
Well, if the data blocks are of fixed size, I'd go for a list, e.g.

#include <list>
using namespace std;

struct DataBlock {
 char data [ FIXED_SIZE];
};

list<DataBlock> lstBlocks;
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 7

Expert Comment

by:KangaRoo
ID: 6857932
I am not clear on what you want. There is data stored in a file and you need it read, and some objects/data initialized from it? If you don't know the size of the entire file, do you know the amount of data that needs to be read?

>> header with and index to 512 blocks of the data which are not necessarily sequential
Can't you use this header then to determine which portions to read

>> but if I dont have any other choice
To sson to tell.
0
 
LVL 1

Expert Comment

by:lidorc
ID: 6859745
Well, I'm not sure about it, cause I really don't know WinCE. Is there a memory maped file in WinCE? if there is, it will be more efficient than the other solutions but platform dependent.
0
 

Author Comment

by:el_rooky
ID: 6860917
I guess there are two parts to my question.
1)How can I load a bunch of data blocks from a file into a memory buffer. I know how to read this data. I am currently reading into a CString. But I rather read it into an array of chars. But since I have limited memory I wanted to use a dynamic array, like Carray which can grow, but I don’t know if that makes sense.

2) Assuming I have read the data into a memory buffer as per jkr’s example,  so all my data is now in an array of chars pBuf, how do can I load my array of parts, bellow, given the location of the first part and the number of parts. For example: the first part record starts at location 102 and there are 20 parts. The parts records are stored sequentially in pBuf.

struct  part
{
int modelNum;
int  partNum;
float cost;
};


part* Parts[MaxParts];


I should add the following:
1)I am new to C++ and the answer is probably very simple
2)I am using VC++6 with MFC
Thanks again for you help
0
 

Author Comment

by:el_rooky
ID: 6861069
I guess there are two parts to my question.
1)How can I load a bunch of data blocks from a file into a memory buffer. I know how to read this data. I am currently reading into a CString. But I rather read it into an array of chars. But since I have limited memory I wanted to use a dynamic array, like Carray which can grow, but I don’t know if that makes sense.

2) Assuming I have read the data into a memory buffer as per jkr’s example,  so all my data is now in an array of chars pBuf, how do can I load my array of parts, bellow, given the location of the first part and the number of parts. For example: the first part record starts at location 102 and there are 20 parts. The parts records are stored sequentially in pBuf.

struct  part
{
int modelNum;
int  partNum;
float cost;
};


part* Parts[MaxParts];


I should add the following:
1)I am new to C++ and the answer is probably very simple
2)I am using VC++6 with MFC
Thanks again for you help
0
 

Author Comment

by:el_rooky
ID: 6921336
I was finally able to get this working. THanks for all your help. What I ended up doing is loading all the data block in a memory buffer and then use memcpy do copy the data into a structured array.

char* dataBuff;
dataBuff = new char[myFile.GetLength()];
for (int n = 0; n < nPartsCount; n++){
  memcpy(*Parts[n],dataBuff,sizeof(part));
}
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 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