File Database & Data Structure :: C++

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

I have a finite array list of a data structure (some char and integers). I can write to a file with the update of the array list. For example, let say the array list holds an array of five data structures. I can write all contents to a file.

While the program can update a file with the latest content, it cannot *read* all data from the file. For example, let say the array list holds five data structures. Let say the array list was full when the program updated the database file. Thus, there are five complete data structures in the database file. When the program reads the database file and copy the data from the file into the same array list, it only reads *the first data structure*. In other words, the program can update the database file with new data structure, but it can only read one data structure from the file at any instances.

Here is how I get it to read data from the database file.

file.read(reinterpret_cast<char *>(instanceofarraylist), sizeofdatabasefile);

What is the best way to solve this problem? I want to copy all context from the database file into the array list.

Last, is this problem related to the fact that the list is an array rather than pointer-base?

Thanks,
Kuphryn
 

m0ti

Senior member
Jul 6, 2001
975
0
0
not sure how to solve your problem, but maybe I can point out a couple of things that will help.

1) in C/C++ arrays are effectively pointer based (consider that an array a. a is effectively a pointer (and can be treated as such) a is just shorthand for (*a + i)

2) You might want to try doing a for loop, updating each member of the array from the file. You may need to provide the location in the file for each one to get the information from the appropriate location. Make sure you handle EOF exceptions.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< n C/C++ arrays are effectively pointer based (consider that an array a. a is effectively a pointer (and can be treated as such) a [j] is just shorthand for (*a + j) >>



i think you ment *(a+j), additionally I'm not sure what you mean by "arrays being effectivly pointers" ...
saying int a[3] and int* a = new int[3] is effectivly not the same and I don't mean in memory allocation sense.

as far as the original problem goes, here's an example of writing arrays of fixed size structs into a file and reading them back into a vector


#include <iostream>
#include <fstream>
#include <vector>

struct S
{
double d;
int i;
float f[2];
char n[32];
};

int main( )
{
int j=0;
S s[] = {
{ 1.7, 1, { 9.1f, 11.2f }, "blue" },
{ 5.2, 2, { 19.1f, 111.2f }, "yellow" },
{ 1.2, 3, { 9.13f, 131.2f }, "red" },
{ 0.0, 0, { 0.0f, 0.0f }, 0 }
};

ofstream out;
out.open("S.bin", ofstream::binary);
while(out.good( ) && s[j].n[0])
out.write(&s[j++], sizeof(S));
out.close( );

S ns;
vector<S> vs;
ifstream in;
in.open("S.bin", ifstream::binary);
while(in.good( ) && in.peek( ) != EOF) {
in.read(&ns, sizeof(S));
vs.push_back(ns);
}
}


edit: i -> j due to tags