Basically I have to load the entire file into the memory, using a dynamically allocated array. The file is basically an adress book, using a structure to contain the name, email and phone number. My question is, will something like:
void *pArray = new Book[bufferSize] (where Book is the name of the structure and the buffer size is a const int) contain copies of the structure itself or is it going to contain different entries from this structure? Let me know if my question was confusing, english is not my native language.. I will try to explain it better.
When you do this (note that void* is just asking for trouble):
Book* pArray = new Book[bufferSize];
pArray is a pointer to an array of Books, in which there are bufferSize elements, and every Book will have been created using the default constructor (i.e. the constructor function will have been run). However the structure is after the default constructor is run is how each one of those will be.
I am sorry, but could you explain about the constructors, since this is not a class, but a structure and I don't know how to write constructors for structs.. code has no classes in it, and this is, well part two of the project I am given, and I have a couple of files with Book entries inside (from part one of my project)..
So, when for example my file contains 3 entries, and I load them into the memory, the first element of the array will hold entry number 1 and so on..
And not only blank copies of the structure?
The difference between a class and a structure is the default behavior of members.
In a class, all members are private, unless explicitly identified as public.
In a structure, all members are public, unless explicitly identified as private.
OKay thanks. Another quick question: If I allocate memory in a void type function, that has no parameters, will this array I allocated continue to exist after the function ends? Because I am wondering if I should put the allocation code inside my main() or inside my file opening function. So far, I tried to use the file opening function (with the allocation code inside the main function), using a referance to the array as parameter, but I keep getting compilation errors.
If I allocate memory in a void type function, that has no parameters, will this array I allocated continue to exist after the function ends?
Any variables declared in a function are destroyed when execution leaves that function. If you want to allocate memory in a function and use that memory somewhere else, you need to return a pointer to the memory. some_type * function_name();
Otherwise, when execution leaves the function, the pointer itself will be destroyed, but the memory that it pointed to will not be freed. This causes a memory leak.
Thanks you for answering! Which funcion should I use in order to copy file contents (structure elements) in the memory ? I can't seem to quite do it with file.read()
It will probably be easier to use the standard libraries "vector" and "algorithm" for what you want to do.
Vector class objects behave exactly like arrays, with the added benefit of being expandible; a vector can be easily resized, while an array cannot.
Vectors can hold any type an array can:
1 2
std::vector<int> v_int; //a vector of ints
std::vector<Book> v_book; //a vector of Books
The algorithm library has many useful algoriths, among these is "copy".
Thank you, but unfortunately this is not an option. It is strictly said that we should use either a dynamic array or a list (but the list option is kind of too hard for me). I already have some code for enlarging the array.. What I need to do now is write a funcion that loads the file, copies its contents (structures) into the memory and then closes it. Sorry for asking so many things, but I am really kind of inexpirienced with this stuff.
Oh, sorry. I didn't know you were not allowed to use those things.
Since you can use structures, why not define one that holds all the variables to be read in from the file?
1 2 3 4 5 6
struct Entry
{
std::string name_;
std::string email_;
long phoneNumber_;
};
Now you can add a loop that first creates a pointer-to-Entry, tries to read some variables from the file and, if the attempt to get input is successful, will set the Entry object's variables to the values read in.
When all that is done, the Entry object is put in an array of Entrys, the pointer-to-Entry is deleted, and the loop repeats.
OK maybe I did not explain this right. For the first part we had to contain the data from the structures in binary files and do things such as change, delete etc.
So I have a couple of files already that have entries from this structure in them, so I dont need to check if they contain entries from this structure. I already have this structure defined from the previous part of the project. For the second part we need to load it into the memory.. Is the loop going to look something like this?
I still have to write an if statement which checks if the array is big enough, and if it is not, it gets resized.
edit: the newEntry is the name of the objects written in the files from the prevoius part of the project
Ok, I found it out. Now a problem I am going to face many times is : how to find out how many non-empty elements does the array hold? In other words, how many entries are there in the book? Because when I try to print the entries to the screen it shows the empty ones too, plus the gibberrish that indicates the end of the file. I need you guys to help me in finding out how many non empty records are already written in the memory, please!
EDIT: Nevermind figured it out.. I had to create an integer in the main() which holds the count of non-empty elements, pass it by reference to the function I have above, set it to zero in the beginning, and then making it equal to the counter (i).. If you can tell me a better solution tho, dont hold back, since (I think) this is quite an ugly one