access violation problem

J

JasBascom

int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering the access
violation problem. I've tried the dot operator to access the structure but that
doesn't appear to work. How can change the syntax and avoid the access
violation error?
 
J

John Carson

JasBascom said:
int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering
the access violation problem. I've tried the dot operator to access
the structure but that doesn't appear to work. How can change the
syntax and avoid the access violation error?


Since most people here lack psychic abilities, I suggest that you tell us
how rec is defined.
 
R

Rolf Magnus

JasBascom said:
int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering the
access violation problem. I've tried the dot operator to access the
structure but that doesn't appear to work. How can change the syntax
and avoid the access violation error?

How would anyone hwere know? What is rec? Where is it defined? It seems
to be a pointer, so where did you let it point to something? What is
the '*' at the beginning of the last line supposed to do?
 
J

JasBascom

rec is the pointer to a union
it is declared thus union Allrecords* rec.
Now that you know what rec is does that help you to help me?
 
J

John Carson

JasBascom said:
rec is the pointer to a union
it is declared thus union Allrecords* rec.
Now that you know what rec is does that help you to help me?

Yes. A pointer simply stores a memory address. Thus rec is meant to store
the address of an instance of Allrecords. However, you never create an
instance of Allrecords (as far as one can tell from the still-limited
information that you have provided), so rec never gets the chance to perform
its proper function. Instead, whatever value rec is storing points to memory
that you don't own and hence you get an access violation when you try to
write to it. You could create an instance of Allrecords with the following:

union Allrecords* rec = new Allrecords;

which allocates memory for an instance of Allrecords and makes rec point to
it. You could also forget about pointers and just declare:

union Allrecords rec_obj;

to create an Allrecords object called rec_obj.

Whether you are accessing the members of this structure correctly with

* rec->Newcrecord.record_type = record_typec;

is again impossible to tell without seeing the definition of Allrecords.
 
D

David Harmon

int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc

Variable "rec" is never declared above.
"Newrecord" is never declared above.
etc.

Post small, simplified but COMPLETE and COMPILEABLE samples so people
don't have to guess what you are talking about.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work correctly?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
J

JasBascom

The full program is follows. Thank you john for pointing out the access
violation error. i also have a problem with the declaring of an fstream object
- sort_file. For some reason my debugger is unable to move beyond that point in
the code.

I would also like to have rec.Newcrecord.record_type assigned the character 'c'
and to toupper the record_type. can you please help in both instances.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>


using namespace std;

struct crecord {
char record_type;
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};


struct irrecord {
char record_type;
char customercode[5];
char partnum[6];
char issue_rec[5];

};


struct drecord {
char record_type;
char customercode[5];
};


int loop = 200;
long offset = 1;

union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords* rec;




void sort_function( union Allrecords* rec, fstream& validdata )
{

union Allrecords *str_ptr1, *str_ptr2, tempstr;


for(int i =0; i< loop; i++)
while( strcmp(str_ptr1.Newcrecord.customercode, '\0') ||
strcmp(str_ptr1.Newdrecord.customercode, '\0') ||
strcmp(str_ptr1.Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2.Newcrecord.customercode, '\0') ||
strcmp(str_ptr2.Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;

}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}

}









int main()
{
const char sorted_file[] = "A:\\514650SDP2.txt";
const char outfile[] = "A:\\514650VDP1.bin";




long offset = 1;
int filesize;
int reccount;



fstream sort_file;
fstream validdata;



sort_file.open("A:\\514650SDP2.txt", ios::eek:ut);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
};

validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
};


validdata.seekg(-offset, ios::end);
filesize = validdata.tellg();
validdata.seekg(offset, ios::beg);

reccount = sizeof(filesize)/sizeof(Allrecords);
rec = new(Allrecords[reccount]);


validdata.read((char*) rec, filesize);//read the whole file.



for(int i =0; i <reccount; i++)
{
switch(rec.Newdrecord.record_type)
{
case 'c':
case 'C':
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(rec, validdata);
default:;
};

};

return 0;

}
 
D

David Harmon

while( strcmp(str_ptr1.Newcrecord.customercode, '\0') ||


Note that '\0', being a const integral expression with a value of zero,
is a valid C++ null pointer. But not what you wanted. What you wrote
is equivalent to

while( strcmp(str_ptr1.Newcrecord.customercode, NULL) ||

while I think what you wanted is

while( strcmp(str_ptr1.Newcrecord.customercode, "") ||
 
D

David Harmon

validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);

Wrong, need bitwise or, not boolean or, should be

validdata.open("A:\\514650VDP1.bin", ios::in | ios::binary);
 
D

David Harmon

for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))

{
OK, I give up on following the logic of your sort function. Especially
with all the comparison tests mixed in with the sort logic. How bad
would it hurt to replace it all with std::sort() ?

bool compare(const Allrecords* left, const Allrecords* right)
{
return strcmp( left->Newirrecord.customercode,
right->Newirrecord.customercode));
}

Then,

std::sort(rec, rec+reccount, compare);

This is probably not the exact right code because I gave up on exactly
following your logic, but how far off is it?
 
J

JasBascom

thank you so far, but i feel the sort is ok, i won't know until I can get the
sort file to work. The sort file does create a text file on the floppy disk,
but when I go to execute the program there is an access violation error
concerning the sort file.
Thanks again for your help, can someone run the program rthrough their own
compiler just to check.
The program is suppose to open a binary file (in this case validdata), and sort
the contents of validdata and then write this to sort_file.
if anyone is feel brave or a little bored can they actually write a program for
me that opens a text file and a binary file and get the contents of the binary
file to the sort_file.
 
J

John Harrison

Comments follow.

I see you didn't follow my earlier advice not to try to do too much at once.
Your post is full of code which does nothing like what you hope it does.
This isn't surprising, programming is hard and you are new at this.

There is a way to avoid wasting your time writing lots of code which will
just have to be thrown away. The way is to write small amounts of code at a
time (literally 3, 4, 5 lines at most), and get those lines working before
you write any more. This is how professionals work, although their
experience means that they can write code in bigger chunks than 3 - 5 lines.

The way to have you tearing your hair out is to write lots of code without
doing any testing at all.

Don't worry newbies never follow this advice. You'll find out the hard way.


JasBascom said:
The full program is follows. Thank you john for pointing out the access
violation error. i also have a problem with the declaring of an fstream object
- sort_file. For some reason my debugger is unable to move beyond that point in
the code.

I would also like to have rec.Newcrecord.record_type assigned the character 'c'
and to toupper the record_type. can you please help in both instances.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>


using namespace std;

struct crecord {
char record_type;
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};


struct irrecord {
char record_type;
char customercode[5];
char partnum[6];
char issue_rec[5];

};


struct drecord {
char record_type;
char customercode[5];
};


int loop = 200;
long offset = 1;

union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords* rec;




void sort_function( union Allrecords* rec, fstream& validdata )
{

union Allrecords *str_ptr1, *str_ptr2, tempstr;


for(int i =0; i< loop; i++)
while( strcmp(str_ptr1.Newcrecord.customercode, '\0') ||
strcmp(str_ptr1.Newdrecord.customercode, '\0') ||
strcmp(str_ptr1.Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2.Newcrecord.customercode, '\0') ||
strcmp(str_ptr2.Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;

}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}

}


This function will crash as soon as it is entered. The problem is the
uninitialised pointers str_ptr1 and str_ptr2. This is the same mistake you
made in main. More seriously this code performs nothing like a sort, not
even close, it should be thrown away.
int main()
{
const char sorted_file[] = "A:\\514650SDP2.txt";
const char outfile[] = "A:\\514650VDP1.bin";




long offset = 1;
int filesize;
int reccount;



fstream sort_file;
fstream validdata;



sort_file.open("A:\\514650SDP2.txt", ios::eek:ut);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
};

validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
};


validdata.seekg(-offset, ios::end);
filesize = validdata.tellg();
validdata.seekg(offset, ios::beg);

What is this? Why seek to one byte before then end of the file? What do you
think that achieves?
reccount = sizeof(filesize)/sizeof(Allrecords);

This is wrong. Should be filesize not sizeof(filesize). This is a good
example of where you are gonig wrong. Obviously you cannot go any further
until you have worked out the number of records in the file. Until you can
do this the rest of your program isn't going to work. So you should have
written the above code, and then STOPPED THERE! Tested your code and seen if
you calculated the number of records correctly. Then you can carry on and
write some more code. At the moment your code doesn't work, and you have no
idea of its the number of records that is wrong, or something completely
different.

rec = new(Allrecords[reccount]);



validdata.read((char*) rec, filesize);//read the whole file.



for(int i =0; i <reccount; i++)
{
switch(rec.Newdrecord.record_type)
{
case 'c':
case 'C':
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(rec, validdata);


This seems a bit confused, are you trying to sort all the records or just
one record? I presume you are trying to sort all the records, but then why
is the sort_function call in a loop?
default:;
};

};

return 0;

}

Seriously the best advice would be to throw this code away and take my
advice to write the program a little bit at a time. You are just heading for
weeks and weeks of frustration any other way.

john
 
J

John Harrison

JasBascom said:
thank you so far, but i feel the sort is ok, i won't know until I can get the
sort file to work.

That's the problem isn't it? Suppose you get the sort file to work, maybe
the changes you make as a result of that, will mean that you have to rewrite
the sort function.

For the record the sort function is not remotely right.
The sort file does create a text file on the floppy disk,
but when I go to execute the program there is an access violation error
concerning the sort file.
Thanks again for your help, can someone run the program rthrough their own
compiler just to check.
The program is suppose to open a binary file (in this case validdata), and sort
the contents of validdata and then write this to sort_file.
if anyone is feel brave or a little bored can they actually write a program for
me that opens a text file and a binary file and get the contents of the binary
file to the sort_file.

Something like this (untested, but please test it your self!)

ifstream validdata("myfile", ios::in|ios::binary);
validdata.seekg(0, ios::end);
filesize = validdata.tellg();
recount = filesize/sizeof(Allrecords);
rec = new Allrecords[reccount];
validdata.seekg(0, ios::beg);
validdata.read(rec, filesize);
validdata.close();

john
 
D

David Harmon

That's the problem isn't it? Suppose you get the sort file to work, maybe
the changes you make as a result of that, will mean that you have to rewrite
the sort function.

Ditto. Comment out the sort function and don't even worry about it
until the program reads and writes the file without sorting it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top