int main()
{
unsignedint *dWords = NULL;
int wordCount = 0;
cout<<"The number of words to load: ";
cin>>wordCount;
genDWords(wordCount,&dWords);//here I allocate memory depending on
//wordCount and change the value of dWords so
//it does not point NULL anymore
//Here I want to write something to save *dWords to a binary file, then
//i want to read from it and access the value(s)
return 0;
}
I have written something like this but it seems to be wrong and I can't tell what exactly...
1 2 3 4 5 6 7 8 9 10 11 12
fstream outB("data.bin", ios_base::binary | ios_base::out);
outB.write((char*)dWords,4*wordCount);
outB.close();
//Here let's say I exit the program and the binary file is created
//Then I start the program again and want to read the same file so I can
//work with the dWords value I've saved into the file from the upper function
fstream inB("data.bin", ios_base::binary | ios_base::in);
inB.read((char*)(&dWords),4*wordCount);
inB.close();
If anyone can give me idea clue or hint please do. I've read all about saving to binary files. The file seems to be saved with the size I expect to but can't read from it or while compiling it gives me the error:
It is always better to post something more complete but I think that your problem is that you are passing the address of the pointer variable which I don't think you want to do. You want to pass the data contained by the variable which is the starting address of the memory block. Take a look at the example code here. http://cplusplus.com/reference/iostream/istream/read/
Well that's what I did, but this will only print the contents of the file. I want each double word I create in gendWords() to be saved in the file. Then when start the program again, i want to be able to read from that same file and from there change the *dWords value. Let's put some more code so it makes sense:
void genDWords(int wC,unsignedint ** wrd)//generates random double words and puts them on the screen
{
if(wC == 0) exit(1);
srand(time(NULL));
*wrd = newunsignedint;
*wrd = (unsignedint *) malloc(4*wC); //allocating memory for the double words
for(int i = 0; i<wC; i++)//fills the array with random double words
{
(*wrd)[i] = (rand() & 0xFFFFFFFF);
};
for(int i = 0; i<wC; i++)//displays the contents of the array
{
cout<<dec<<i+1<<". ";
cout<<hex<<(*wrd)[i]<<endl;
};
}
int main()
{
unsignedint *dWords = NULL;//declaring *dWords to point NULL
int wordCount = 0;
int act;
do
{
cout<<"1.Generate random double words."<<endl;
cout<<"2.Calculates the average value of each double word."<<endl;
cout<<"3.Save to file."<<endl;
cout<<"4.Read from file."<<endl;
cout<<"Choose an action: ";
cin>>act;
switch (act)
{
case(1):
cout<<"\033[22;37mThe number of words to load: ";
cin>>wordCount;
genDWords(wordCount,&dWords);//here I create the dWords and change the value of *dWords by passing it by reference so I
//can use its new value in another function in main()
break;
case(2):
getAvgWords(wordCount,dWords);//here I use the new *dWords value and display something on the screen
break;
case(3):
saveTo(wordCount,dWords);//Here I want to save *dWords which is basically an Array of double words into a binary file
break;
case(5):
loadFrom(wordCount,&dWords);//after i quit the program and start it again the inital value of *dWords is NULL so
//here i want to load this file where I stored the *dWords, (the array) pass it to *dWords so it
//receives the values and be able to use it in function getAvgWords()
break;
}
} while (act!=0);
return 0;
}
I'm sorry that this is too long but it was the only way to explain what I want to do.
Thanks.