file binary data in array

I try to extract some data of a binary file, and put this data in a array, I have writed this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// VC5.cpp : Defines the entry point for the console application.
//

/clr
#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cstring>
#include "stdio.h"
#include "stdlib.h"
#include <vcclr.h>
#include <vector>

using namespace std;
using namespace System;



FILE *fp1;


//Variable
char * prt_info;
long recl,lenr,numf,numx,numy;
float xmin,xmax,ymin,ymax;


//Struct
struct Arr
{
float *X;
float *Y;
float *Z;
float *A;
float *B;
float *C;
float *F;
long numofRec;
};

int main()
{
System::String^ str;
//Number of records
int num_rec = 0;
int i=0;
//Declare struct
Arr info[1];

      pin_ptr<const wchar_t> wch = PtrToStringChars(str);

      // Convert to a char*
      size_t origsize = wcslen(wch) + 1;
      const size_t newsize = 1000;
      size_t convertedChars = 0;
      char nstring[newsize];
      wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
      //strcat_s(nstring, " (char *)");

errno_t err;
//Read from data file


if((err=fopen_s(&fp1,"C:\Sample\raysdetector.dis","rb"))!=0)
fread(&recl,sizeof(recl),1,fp1);
lenr=recl/256/4;
fseek(fp1,28+24,0);
fread(&numf,sizeof(numf),1,fp1);
fseek(fp1,3*28+16,0);
fread(&ymin,4,1,fp1);
fread(&ymax,4,1,fp1);
fread(&numy,4,1,fp1);
fseek(fp1,4*28+16,0);
fread(&xmin,4,1,fp1);
fread(&xmax,4,1,fp1);
fread(&numx,4,1,fp1);

for (long i=1;i<numy;i++)
{
fread(info[i].X,4,1,fp1);
fread(info[i].Y,4,1,fp1);
fread(info[i].Z,4,1,fp1);
fread(info[i].A,4,1,fp1);
fread(info[i].B,4,1,fp1);
fread(info[i].C,4,1,fp1);
fread(info[i].F,4,1,fp1);
info[i].numofRec=i;
printf("info[i].numofRec");
}

fclose(fp1);
}


But I receive this error (help me, please):

'VC5.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll'
'VC5.exe': Loaded 'C:\WINDOWS\SysWOW64\rsaenh.dll'
'VC5.exe': Loaded 'C:\WINDOWS\SysWOW64\psapi.dll'
'VC5.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\ba0e3a22211ba7343e0116b051f2965a\System.ni.dll'
Unhandled exception at 0x102670ef (msvcr90d.dll) in VC5.exe: 0xC0000005: Access violation reading location 0x00000000.
Error 1 error C2059: syntax error : '/' c:\users\kevin\documents\visual studio 2008\projects\testing\testing\testing.cpp 4
Error 2 fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afx.h 24

That is what my compiler just said so maybe you are using a library that can't work with one of your other libraries?
I have modified different line, no error in debug, but at the compilation the result is:

An unhandled exception of type 'System.AccessViolationException' occurred in VC6.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

there is the new code (maybe the declaration of dimension?)

// VC6.cpp*: définit le point d'entrée pour l'application console.
//

/clr
#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cstring>
#include "stdio.h"
#include "stdlib.h"
#include <vcclr.h>
#include <vector>
//#include <string>

using namespace std;
using namespace System;

FILE *fp1;
//string ^str;
std::string;

//System::String^ str;

//Variable
char * prt_info2;
int i,nb_step_Y,nb_step_X,j,k,cc,dd,n;
long recl,lenr,numf,numx,numy;
float xmin,xmax,ymin,ymax;
//MAX=100;
//Struct
//typedef struct {
//float abscisse;
//float ordonnee;
//} tPoint;

//float X,Y,Z,A,B,C,F;

struct info{
float *X;
float *Y;
float *Z;
float *A;
float *B;
float *C;
float *F;
};

info info2,bar,juju;

int main()
{
String ^str;
int num_rec = 0;

pin_ptr<const wchar_t> wch = PtrToStringChars(str);

// Convert to a char*
size_t origsize = wcslen(wch) + 1;
const size_t newsize = 1000;
size_t convertedChars = 0;
char nstring[newsize];
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
//strcat_s(nstring, " (char *)");



errno_t err;
//Read from data file
if((err=fopen_s(&fp1,"C:\\Sample\\project2DAT.dat","rb"))!=0)
fread(&recl,sizeof(recl),1,fp1);
lenr=recl/256/4;
fseek(fp1,28+24,0);
fread(&numf,sizeof(numf),1,fp1);
fseek(fp1,3*28+16,0);
fread(&ymin,4,1,fp1);
fread(&ymax,4,1,fp1);
fread(&numy,4,1,fp1);
fseek(fp1,4*28+16,0);
fread(&xmin,4,1,fp1);
fread(&xmax,4,1,fp1);
fread(&numx,4,1,fp1);

for (long i=0;i<2;i++)
{
fread(&info2.X[i],4,1,fp1);
fread(&info2.Y[i],4,1,fp1);
fread(&info2.Z[i],4,1,fp1);
fread(&info2.A[i],4,1,fp1);
fread(&info2.B[i],4,1,fp1);
fread(&info2.C[i],4,1,fp1);
fread(&info2.F[i],4,1,fp1);
}

fclose(fp1);

//build the step for smaller grid
nb_step_Y=(int)ymax-(int)ymin;
nb_step_X=(int)xmax-(int)xmin;

//build the array with the lines by smaller grid
cc=0;

for (long j=0;j<nb_step_Y;j++)
{
for (long k=0;k<nb_step_X;k++)
{
cc=cc+1;
dd=0;
for (i=0;i<numx;i++)
{
if ((xmin-0.5*k < info2.X[i] < xmin+0.5*k)&&(ymin-0.5*j < info2.Y[i] < ymin+0.5*j))
dd=dd+1;
juju.X[dd]=info2.X[i];
juju.Y[dd]=info2.Y[i];
juju.Z[dd]=info2.Z[i];
juju.A[dd]=info2.A[i];
juju.B[dd]=info2.B[i];
juju.C[dd]=info2.C[i];
juju.F[dd]=info2.F[i];

for (cc=1;cc<dd;cc++)
{
n=k*j;
bar.X[n] = juju.X[cc];
bar.Y[n] = juju.Y[cc];
bar.Z[n] = juju.Z[cc];
bar.A[n] = juju.A[cc];
bar.B[n] = juju.B[cc];
bar.C[n] = juju.C[cc];
bar.F[n] = juju.F[cc];
};
bar.X[n] = bar.X[n]/dd;
bar.Y[n] = bar.Y[n]/dd;
bar.Z[n] = bar.Z[n]/dd;
};
};
};
// recording of result in file txt
FILE* fichier = NULL;
fichier = fopen("C:\\Sample\\donnees.txt", "a+");

if (fichier != NULL)
{
fputs("Récapitulatif :\n", fichier);
for (cc=1;cc<dd;cc++)
{
fprintf(fichier, "*", bar.X[cc], "*", bar.Y[cc], "*", bar.Z[cc], "*", bar.A[cc], "*", bar.B[cc], "*", bar.C[cc], "*", bar.F[cc]);
}
fclose(fichier); // On ferme le fichier qui a été ouvert
cout << "writing is ok\n";
}
else
{
// errror
printf("Impossible to open the file");
}
system("pause");
return 0;
};
Topic archived. No new replies allowed.