Re: access violation problem

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 18:26:25 -0000

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" <jasbascom@aol.com> wrote in message
news:20040220115746.16083.00000078@mb-m11.aol.com...
> 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[i].Newcrecord.customercode, '\0') ||
> strcmp(str_ptr1[i].Newdrecord.customercode, '\0') ||
> strcmp(str_ptr1[i].Newirrecord.customercode, '\0'))
> {
> str_ptr2 = str_ptr1 + 1;//set to next element.
>
> for( i=0; i<loop; i++)
> while( strcmp(str_ptr2[i].Newcrecord.customercode, '\0') ||
> strcmp(str_ptr2[i].Newdrecord.customercode, '\0'))
> {
> for(int i=0; i<loop; i++)
> if( strcmp( str_ptr1[i].Newirrecord.customercode,
> str_ptr2[i].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::out);
> 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[i].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



Relevant Pages