Re: write to a binary file
Before posting, please format your code and use code tags. Go Advanced, select the code and click '#'
Code:
int slen;
slen = 2;
int joly[slen] = { 1,2,3,4,5 };
Array sizes are computed at compile time and not runtime. So array size needs to be a constant that the compiler can evaluate.
Code:
const int slen = 2;
int joly[slen] = { 1,2 };
This says that the array joly has 2 elements initialised to 1 and 2.
Re: write to a binary file
Quote:
Originally Posted by
laftaxxx
This is what I have so far
My function does not work and my compiler says my int array is not initialized
That is not the only problem.
Code:
void arrayToFile(ofstream tak,
Streams are not copyable, therefore they cannot be passed by value as you're doing here. They must be passed by reference (or a pointer to the stream object must be passed).
Code:
void arrayToFile(ofstream& tak,
Regards,
Paul McKenzie
Re: write to a binary file
Thanks Paul I have made the corrections and still get errors.
Corrected program
#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;
void arrayToFile(ofstream &tak, int *arr[], int size )
{
for (int i = 0; i<size; i++)
{
tak.write((char *)arr[i], sizeof(arr[i]));
}
}
int main()
{
const int slen = 2;
char joly[slen] = { 1,2, };
ofstream outfile;
outfile.open("data.bin",ios::out | ios::binary);
arrayToFile(&outfile, *joly, slen);
return 0;
}
error message
C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid initialization of non-const reference of type 'std::ofstream& {aka std::basic_ofstream<char>&}' from an rvalue of type 'std::ofstream* {aka std::basic_ofstream<char>*}'|
C:\Users\Public\Documents\lkh\main.cpp|9|error: in passing argument 1 of 'void arrayToFile(std::ofstream&, int**, int)'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 3 seconds) ===|
Re: write to a binary file
Since the first parameter of arrayToFile is of type reference to ofstream, you should not pass a pointer as an argument, i.e., pass outfile, not &outfile.
By the way, please post code in [code][/code] bbcode tags.
Re: write to a binary file
thanks laserlight. I passed the outfile, got the following error.
C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid conversion from 'char' to 'int**' [-fpermissive]|
C:\Users\Public\Documents\lkh\main.cpp|9|error: initializing argument 2 of 'void arrayToFile(std::ofstream&, int**, int)' [-fpermissive]|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 5 seconds) ===|
Re: write to a binary file
Quote:
Originally Posted by
laftaxxx
thanks laserlight. I passed the outfile, got the following error.
C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid conversion from 'char' to 'int**' [-fpermissive]|
C:\Users\Public\Documents\lkh\main.cpp|9|error: initializing argument 2 of 'void arrayToFile(std::ofstream&, int**, int)' [-fpermissive]|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 5 seconds) ===|
The data type that's expected is int, but you changed your array type to char.
But before going on, where did you get this code from, or are you trying to write something yourself "off the cuff"?
I ask this, since all the code I know of that teaches you passing arrays as parameters do not show doing what you're doing. If you're passing a one dimensional array, you pass the array name only. The receiving function then would have a prototype as the following:
Code:
void arrayToFile(std::ofstream&, int*, int)
or
Code:
void arrayToFile(std::ofstream&, int[], int)
Both of these prototypes are exactly the same. Arrays decay to pointers when you pass the name of the array. In reality, when you "pass arrays", the address of the first item in the array is actually passed.
Regards,
Paul McKenzie
Re: write to a binary file
Your arrayToFile function also has a problem with the write.
Code:
void arrayToFile(ofstream &tak, int arr[], int size )
{
for (int i = 0; i<size; i++)
{
tak.write((char *)&arr[i], sizeof(arr[i]));
}
}
write() requires a pointer to the memory but arr[i] returns the actual value, not the memory address. You need &arr[i] to obtain the required address.
Re: write to a binary file
Thanks Paul
So my function should be
void arrayToFile(const char * filename, int*array, int size)
sorry about the char instead of int...oversight.
Re: write to a binary file
Have you read post #8?????