Problem in filling int array using binary file
I am trying to do binary filling
This is the Constructor code, you will see a couple of integer arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Doc::Doc ( int useridValue , string firstnameValue , string lastnameValue , string deptValue
, int dayinhourValue[] , int dayinminValue[] ,int dayouthourValue[] , int dayoutminValue[]
, int timeperpatValue , int appValue[] , int applimValue[] )
{
setuserid (useridValue);
setfirstname (firstnameValue);
setlastname (lastnameValue);
setdept (deptValue);
setdayinhour ( dayinhourValue);
setdayinmin ( dayinminValue);
setdayouthour ( dayouthourValue);
setdayoutmin ( dayoutminValue );
}
|
the problem is that when it calls
1 2 3 4
|
setdayinhour ( dayinhourValue );
setdayinmin ( dayinminValue );
setdayouthour ( dayouthourValue );
setdayoutmin ( dayoutminValue );
|
it gives an error:
Unhandled exception at 0x010b2b37 in hms.exe: 0xC0000005: Access violation reading location 0x00000000. |
And it highlights the member funtion:
1 2 3 4 5 6 7
|
void Doc::setdayinhour ( int dayinHOUR[] )
{
for(int i=0;i<7;i++)
{
dayinhour[i]=dayinHOUR[i];//highlighted area
}
}
|
I am using this code for filling:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void main ()
{
Doc r;
ofstream outDoc ( "docdata.dat" , ios::out | ios::binary );
if ( !outDoc )
{
cerr << "File Could Not Be Opened" << endl;
cin.get();
exit (0);
}
for ( int i=0;i<1000;i++ )
{
outDoc.write(reinterpret_cast <const char *> (&r), sizeof(Doc));
}
cin.get();
}
|
can anyone help plz...
Show your "Doc" class code.
From the error I can tell the problem comes from the fact you're reading or writing an uninitialized pointer that is at NULL.
Please show us the class declaration for Doc and also the default constructor for Doc. I suspect you're not getting past line 3.
This is the Class and Its Constructor prototype:
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
|
class Doc
{
private:
int userid;
char firstname[20];
char lastname[20];
char dept[20];
int dayinhour[7];
int dayinmin[7];
int dayouthour[7];
int dayoutmin[7];
int timeperpat;
int app[7];
int applim[7];
public:
Doc ( int = 0 , string = "" , string = "" , string = "" , int[] = 0 , int[] = 0 , int[] = 0, int[] = 0
, int = 0 , int[] = 0 , int[] = 0 );
void setuserid ( int );
int getuserid ( );
void setfirstname ( string );
string getfirstname ( );
void setlastname ( string );
string getlastname ( );
void setdept ( string );
string getdept ( );
void setdayinhour ( int[] );
void setdayinmin ( int[] );
void setdayouthour ( int[] );
void setdayoutmin ( int[] );
void settimeperpat ( int );
int gettimeperpat ( ) ;
void setapp ( int[] );
void setapplim ( int[] );
int getapplim ( ) ;
Doc getobj ( );
};
|
And this is the constructor code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Doc::Doc ( int useridValue , string firstnameValue , string lastnameValue , string deptValue
, int dayinhourValue[] , int dayinminValue[] ,int dayouthourValue[] , int dayoutminValue[]
, int timeperpatValue , int appValue[] , int applimValue[] )
{
setuserid ( useridValue );
setfirstname ( firstnameValue );
setlastname ( lastnameValue );
setdept ( deptValue );
setdayinhour ( dayinhourValue );
setdayinmin ( dayinminValue );
setdayouthour ( dayouthourValue );
setdayoutmin ( dayoutminValue );
settimeperpat ( timeperpatValue );
setapplim ( applimValue );
}
|
And here the problem starts:
1 2 3 4 5 6 7 8
|
void Doc::setdayinhour ( int dayinHOUR[] )
{
for(int i=0;i<7;i++)
dayinhour[i] = dayinHOUR[i];
}
|
I am getting the problem that I am reading or writing an uninitialized pointer that is at NULL.
What is the solution in this case? Sorry I am new to c++
Last edited on
Your declaration of your default constructor at line 20, defaults arguments 5-11 to a NULL pointer if not explicity passed.
When you invoke the default constructor at lne 3 of your main, the call to the constructor is effectively:
|
Doc r (0,"","","",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
|
This causes a problem in Doc::setdayinhour when you try to reference dayinHOUR[] which is a null pointer.
Thank You Very Much :)
Topic archived. No new replies allowed.