Article View: alt.comp.lang.learn.c-c++
Article #734General protection exception?
From: Mark Hayward
Date: Wed, 02 Dec 1998 00:00
Date: Wed, 02 Dec 1998 00:00
82 lines
3843 bytes
3843 bytes
Hi,
I'm learning C (Not C++ yet) and I'm having some trouble with a function.
I'm using Borland Turbo C++ 4.5. Basically it's part of a database program
which stores details about computers. The function is meant to save each
individual record as a seperate file called "rec[record number goes in
here]". I found that I couldn't increment the number in the filename
directly, by doing something similar to:
for(y=0; y<ctr; y++)
{
*filename="rec%d",y;
fopen(filename);
/*Save data operations here*/
}
The only way I could think of to do it was to create another file, and just
write to that file the filename with relevant number, and then read that
from the file to and fprintf to that filename. But, just like a true newbie,
I got something wrong and the compiler says:
General protection exception
0x5117:0xDE4
Processor fault
What's going on?
The code I wrote is as follows:
void save_data(void)
{
int z=0;
FILE *fp;
FILE *incrementor;
for (z=0; z<ctr; z++)
{
char filename[10];
char incfilename[15]="c:\\increm.dat";
if ( (incrementor=fopen (incfilename, "r+") ) ==NULL) /* Check for
successful
file opening */
{
fprintf(stderr, "Error opening incrementation data file!!");
}
fprintf(incrementor, "rec%d.jim",z);
fgets(filename, BUFFERLENGTH, incrementor);
if ( ( fp=fopen (filename, "r+") ) == NULL) /* Check for successful file
opening */
{
fprintf(stderr, "Oooh \'eck! Critical error opening file!!");
exit(1);
}
/* Save data to file */
fprintf(fp, "\nCPU speed: %dMHz", details[z].speed);
fprintf(fp, "\nRAM: %dMb", details[z].ram);
fprintf(fp, "\nCache size: %dKb", details[z].cache);
fprintf(fp, "\nMonitor size: %d\"", details[z].monitor);
fprintf(fp, "\nSound and video cards: %s", details[z].cards);
fprintf(fp, "\nPeripherals attached: %s", details[z].peripherals);
fprintf(fp, "\nSoftware installed: %s", details[z].software);
fprintf(fp, "\nCD-ROM drive speed: %d", details[z].cd_speed);
fprintf(fp, "\nMotherboard speed: %dMHz", details[z].moth_speed);
fprintf(fp, "\nPrice: �%d", details[z].price);
fclose(fp); /* Close file */
}
As you can see, my code checks successful file opening and when it stops,
the message "Error opening incrementation file!!" is displayed, which means
that fopen returned NULL. Why would this happen?
Also could anyone tell me how to make a program that runs in DOS with Turbo
C++ 4.5?
Please e-mail all replies.
Many thanks,
Jim
P.S.
If you can think of a better way to perform the operations in the function,
could you please tell me? This was the only way I could think of.
Technical Details
Message-ID:
<7447iu$ldg$1@newnews.global.net.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f79ee,8329580fde881798,start X-Google-Attributes: gidf79ee,public X-Google-Thread: 107276,8329580fde881798,start X-Google-Attributes: gid107276,public X-Google-Thread: 109fba,8329580fde881798,start X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,8329580fde881798,start X-Google-Attributes: gid114592,public X-Google-Thread: 1122df,8329580fde881798,start X-Google-Attributes: gid1122df,public X-Google-Thread: 10caf2,8329580fde881798,start X-Google-Attributes: gid10caf2,public X-Google-Thread: 1158f7,8329580fde881798,start X-Google-Attributes: gid1158f7,public X-Google-Thread: 10c73c,8329580fde881798,start X-Google-Attributes: gid10c73c,public From: "Mark Hayward" <mhayward@globalnet.co.uk> Subject: General protection exception? Date: 1998/12/02 Message-ID: <7447iu$ldg$1@newnews.global.net.uk> X-Deja-AN: 417964881 Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,de.comp.lang.c++,fj.lang.c++,it.comp.lang.c++,relcom.fido.su.c-c++,relcom.fido.su.c-c++.visualage,sdsu.c++,utexas.class.cs105.c++ X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3