This code works fine. I don't know what I can do in order to display the entire file. As you can see the array is appended to the file. So if I run this program twice the file will have 200 random generated numbers...and the program should show 200 values, but it only displays the array size of 100. Any ideas?
I understand that. The only problem is that in order to read all of it I need to know the size binaryio.read(reinterpret_cast<char *>(&result), sizeof(result));
So I need to know the appropriate size before I can read the file, and eof() does not give the size. I've tried seekg() then tellg() to get the position, but that's not necessarily how many values are in the file. It's pretty much returning the size "bits" of the file. Is there another way of reading the file? That way I can then run a while loop until the end of file..
I changed hte SIZE to 5 so when I run it the first time there are only 5 values in the file. However, the file is 25 bytes...so the length returns 25 in size. Also not sure why I'm getting the memory address instead of the actual value.
binaryio.seekg (0, ios::end);
length = binaryio.tellg();
binaryio.seekg(0); //to return the file pointer to the beginning
int *result = newint [length];
// Display array
for (int i = 0; i < length; i++)
{
//you only read one int at a time
binaryio.read(reinterpret_cast<char *>(&result[i]), sizeof(result[i]));
cout << result[i] << " ";
}
binaryio.seekg (0, ios::end);
length = binaryio.tellg() / sizeof(int);
binaryio.seekg(0); //to return the file pointer to the beginning
int *result = newint [length];
// Display array
for (int i = 0; i < length; i++)
{
//you only read one int at a time
binaryio.read(reinterpret_cast<char *>(&result[i]), sizeof(result[i]));
cout << result[i] << " ";
}
Forgot that the file size = element size * element count, and we need element count. My bad.
Hmm..the first result is returning correct, but the rest are addresses. Also I get one warning when compiling the code. It's not liking length = binaryio.tellg() / sizeof(int);
warning C4244: '=' : conversion from 'std::streamoff' to 'int', possible loss of data
Here's code output: Also SIZE is only 5..so there should only be 5 results in the file. Underline are not correct for some reason.
389 2526412 64343040 -640942080 -872415201 415 Press any key to continue . . .
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
constint SIZE = 100; // Array size
fstream binaryio; // Create stream object
// Write array to the file
binaryio.open("Exercise13_5.dat", ios::out | ios::app | ios::binary);
int arr[SIZE];
srand((unsigned)time(0));
// Write to array
for (int i = 0; i < SIZE; i++)
{
arr[i] = (rand()%1000); // Random number up to 1,000
}
binaryio.write(reinterpret_cast<char *>(&arr), sizeof(arr));
binaryio.close();
// Read array from the file
binaryio.open("Exercise13_5.dat", ios::in | ios::binary);
binaryio.seekg (0, ios::end);
int length = binaryio.tellg() / sizeof(int);
binaryio.seekg(0); //to return the file pointer to the beginning
int *result = newint [length];
// Display array
for (int i = 0; i < length; i++)
{
//you only read one int at a time
binaryio.read(reinterpret_cast<char *>(&result[i]), sizeof(result[i]));
cout << result[i] << " ";
}
return 0;
}
Yes, that does work. I see where I had messed up. On line 25 I had binaryio.write(reinterpret_cast<char *>(&arr[i]), SIZE);
Which was storing only one value in the array. So of course I was getting memory addresses for the rest of the indexes. The random numbers output fine now..I do still get this warning though
c:\users\kraigballa\desktop\college\isu\spring 2011\cs 182\example\binaryarrayio.cpp(36): warning C4244: 'initializing' : conversion from 'std::streamoff' to 'int', possible loss of data
on gcc. May be your library is differently implemented. Either way, a static cast to int of the result from tellg should kill the warning, unless you want to keep it as alerter.
Alright thanks for your help. I'm using Visual Studio 2010 on Windows 7. By the way what does binaryio.tellg() / sizeof(int); this do? Are you dividing it by 4 bytes as that is the size of an int..