PDA

Click to See Complete Forum and Search --> : Illegal operation


MPrestonf12
Mar 3rd, 2001, 02:29 PM
In this code used to read the contents of a text file I get a illegal operation at the very end right. Here the code

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

FILE *fp;

class myclass{
public:
void openfile(char *path);
};

int main()
{
char retval;

myclass s;

cout <<"Enter a path to open \n";

cin.getline(&retval, 30, '\n');

s.openfile(&retval);

system("pause");

return 0;
}

void myclass::openfile(char *path)
{
char ch;

if((fp = fopen(path, "r")) ==NULL)
cout << "Cannot open file ERROR";

ch = getc(fp);

while (ch!=EOF){
putchar(ch);
ch = getc(fp);
}
fclose(fp);
}

*How do I embed code in a post? Thanks for your help.

parksie
Mar 3rd, 2001, 02:38 PM
You're getting an error because you haven't allocated a buffer for the string. You used char retval. That only has space for one character, you need something like char retval[31] in order to have space for 30 characters and the terminating NULL ('\0').

To embed code use tags.

Warmaster199
Mar 3rd, 2001, 05:00 PM
I've also got an Illegal Operation for one of my programs that I made in PowerC. It's supposed to do some calculation with floating-point. Whenever it gets to the floating-point part, I get an Illegal operation. I worked around it, but I would like to know what's wrong. Is it my compiler(PowerC), or my CPU(AMD486DX-2 80MHz)?

parksie
Mar 3rd, 2001, 05:14 PM
What sort of FLOP was it?

Warmaster199
Mar 3rd, 2001, 07:06 PM
This is the lines the illegal op happens:

int diskspace; /*Bytes free got by getdiskfree(); */
int diskfree;

printf("You have %d kbytes left on you drive", diskfree);

printf("Your drive is %f percent free", diskspace/diskfree*100); /* Line the illegal op occurs */


Any ideas?

parksie
Mar 3rd, 2001, 07:12 PM
That's because printf is trying to interpret an int as a float since you didn't explicitly cast it.
Try this:

printf("Your drive is %f percent free", ((float)(diskspace *100.0f) / (float)diskfree));

Just out of interest, is PowerC 16-bit? Because a float is 4 bytes, and a 16-bit int is only 2. It's just a hunch...but I think that if you compiled it 32-bit there wouldn't be an error.

Warmaster199
Mar 3rd, 2001, 09:44 PM
PowerC... I don't know if it's 16 bit. It's date says 1989-1995 by MIX software. It was around when the 386 and 486 was out so I'm guessing 32bit. I looked at the arguments for PowerC and there are 2 options for CPU - Compile for 80186 and Compile for 80286. It also says something about 8086 FPU stuff... Yeah. I think it is 16bit, now that I think of it. The good thing about PowerC is that it's very small. All the libraries/binaries/includes take up slightly more than a meg. You can pretty much take it anywhere where there's a PC with a floppy drive!

HarryW
Mar 3rd, 2001, 11:00 PM
Anything designed for DOS is 16-bit, although you could get 32-bit DOS extenders like DOS4GW which made it nearly a 32-bit environment.

parksie
Mar 4th, 2001, 10:06 AM
Yeah. I think Watcom C/C++ came with a copy.

Warmaster199
Mar 4th, 2001, 10:07 AM
Is there any source code out there that any of you know about which will make DOS 32-bit?(Like DOS4GW) I've already got DOS4GW(for a game called C-Dogs), but source code could be interesting because then you can design something similar.