Hi,
I am writing an array data to a file. The data I am getting is from some sensor and need to be written in real time. I have manged to write it but I am having problem when I try to read it, as I cannot position the pointer to read 'read_ampl'. So three questions:
1. Is this the way I am writing is fast so that I do not drop frames.
2. Am I ok in writing part as I believe I am appending two arrays one after the other.
3. I can read the first array 'read_dist' but cannot read the 'read_ampl' as I am positioning properly.
Any help please.!!!
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include<fstream>
#include "sdk.h"
using namespace std;
void WriteToFile(const char *filename,Handle hnd,int ArraySize);
int _tmain(int argc, _TCHAR* argv[])
{
static const char filename[] = "data.dat";
int ArraySize;
Handle hnd; // connection handle
ArraySize=3072;
WriteToFile(filename, hnd, ArraySize);
return 0;
}
void WriteToFile(const char *filename, Handle hnd, int ArraySize)
{
double read_dist[3072], read_ampl[3072];
int res,i;
double *dist, *ampl;
res = Update (hnd);
if (res != OK) cout<<"No Update";
res = Distances (hnd, (void**)&dist);
if (res != OK) cout<<"No Distance";
res = Amplitudes (hnd, (void**)&l);
if (res != OK) cout<<"No Amplitude";
std::ofstream fileout(filename,ios::out | ios::binary );
if(!fileout)
{
cout << "Cannot open file to write.\n";
}
fileout.write((char *) dist, 8*ArraySize); //size of double array
fileout.write((char *) ampl, 8*ArraySize);
fileout.close();
ifstream filein(filename, ios::in | ios::binary);
if(!filein) {
cout << "Cannot open file to read.\n"; }
filein.read((char *) &read_dist, sizeof read_dist);
filein.read((char *) &read_ampl, sizeof read_ampl);
for(i = 0; i <ArraySize; i++) // show from file
cout << "Distance: "<<read_dist[i] <<endl;
cout << "Amplitude: "<<read_ampl[i] <<endl;
filein.close();
}