Closed Thread Icon

Topic awaiting preservation: C++ problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12551" title="Pages that link to Topic awaiting preservation: C++ problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: C++ problem <span class="small">(Page 1 of 1)</span>\

 
OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-12-2002 22:27

This is my first program in ages.
But I'm having some problems with Exception Handlers. I get this error message:



code:
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
AnsiString filnamn = Edit1->Text;
FILE *stream;

fopen(filnamn.c_str(),"r");

short int val;
fread(&val,sizeof(val),1,stream);


fclose(stream);

}
//---------------------------------------------------------------------------

Rahly
Bipolar (III) Inmate

From: Michigan
Insane since: Jul 2002

posted posted 12-13-2002 00:03

thats an easy one


you need this line

stream = fopen(filenamn.c_str(),"r");

you need to bring back that object so that you can pass it to the fread function... otherwise you passing an uninitialize value and jumping to an unknown memory space.

Rahly

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-13-2002 15:04

Thanks for the Help!
I'm pretty new to c++ so that's why I make silly mistakes!
/OlssonE

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-13-2002 16:29

I have a binary file and I want to read the header using this structure. How would I do this?


code:
typedef struct _Header	        Header;

struct _Header
{
short version;
short count;
};



[This message has been edited by OlssonE (edited 12-15-2002).]

[This message has been edited by OlssonE (edited 12-15-2002).]

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-15-2002 17:03

*updated Q*

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-19-2002 20:38

Ok, error message problem again!


code:
void readHeader (AnsiString file)
{

FILE* stream;
AbrHeader header;

stream = fopen(file.c_str(),"rb");
fread(&header,sizeof(header),1,stream);

Form1->count->Text = header.count;
Form1->version->Text = header.version;
fclose(stream);

}



[This message has been edited by OlssonE (edited 12-19-2002).]

Rahly
Bipolar (III) Inmate

From: Michigan
Insane since: Jul 2002

posted posted 12-21-2002 05:58

Can't you locate the line its on?.... or give more information... i don't know what AbrHeader is... try commenting out your read and see if it runs?.. i mean.. if you don't have simple debuging skills like process of elimination to find out what line exactly is causing the problem.. your not going to be an effective programmer ... comment out each line... until the error goes away... if it then you comment out ever line.. and it is still happening.. .then your bug is somewhere else ..

Rahly

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-21-2002 19:29

Thanks for the pointers Rahly!
I commented out the code and I found the problem! I'm really confused!

code:
Form1->count->Text = header.count;



and AbrHeader is an structure:

code:
struct _AbrHeader
{
short version;
short count;
};

Rahly
Bipolar (III) Inmate

From: Michigan
Insane since: Jul 2002

posted posted 12-21-2002 23:16

ok... now thats much better... your problem seems to be that your trying to put a "number" into a "string" variable.... there should be a function to change the number to a string.

Please Note that this will also happen with your next line too....

Rahly

[This message has been edited by Rahly (edited 12-21-2002).]

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-23-2002 16:18

I fixed that and still it doesn't work???
here is my code

code:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <fstream.h>
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


typedef struct _AbrHeader AbrHeader;
typedef struct _AbrBrushHeader AbrBrushHeader;
typedef struct _AbrSampledBrushHeader AbrSampledBrushHeader;

struct _AbrHeader
{
short version;
short count;
};

struct _AbrBrushHeader
{
short int type;
int size;
};

struct _AbrSampledBrushHeader
{
int misc;
short spacing;
char antialiasing;
short bounds[4];
int bounds_long[4];
short depth;
bool wide;
};


void readAbr (AnsiString file);
void readAbr(AnsiString filename)
{

FILE* stream;
AbrHeader header;

stream = fopen(filename.c_str(),"rb");
fread(&header,sizeof(AbrHeader),1,stream);
Form1->version->Text = IntToStr(header.version);
Form1->count->Text = IntToStr(header.count);

fclose(stream);

}
/*
ifstream fin(file.c_str(),ios_base::binary);

AbrHeader header;
char buffer;
fin.get(
Form1->version->Text = header.version;
Form1->count->Text = header.count;
fin.close();

*/

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Edit1Change(TObject *Sender)
{
if (Edit1->Text != "*.abr")
{


readAbr(Edit1->Text);


}
}
//---------------------------------------------------------------------------

OlssonE
Maniac (V) Inmate

From:  Eagleshieldsbay, Sweden
Insane since: Nov 2001

posted posted 12-28-2002 22:33

It seems like I'm stuck!
If anyone has an example how to read a file in binary mode using a structure.
Maybe you can tell me what I'm doing wrong?
I have read some basic C about reading files in binary mode but still I can't make it work.
I get an memory access error using the code above.
Has the error something to do with that the file structure and the structure I have created is different?

/OlssonE

« BackwardsOnwards »

Show Forum Drop Down Menu