954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

General Protection Exception Error Please Help!

Hello,
This is my first post. Anyway.. I am having trouble with my c++ program. My assignment is to read from a file a 2-Dimensional array. However, when attempting to do so with my code, which i'm guessing is faulty, I keep getting this error while using Borland Turbo C++ 4.5:

General Protection Exception
0x9CF7:0x4D58
Program2(1) 0x9CF:0x4D58 Processor Fault

Here is my code and I have also attached my files:

#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <stdlib.h>
#include <fstream.h>
#include <ctype.h>

int grade[15][6];

/********************************************************/
void fileopen()
{
  char ch;
  fstream Infile;


 Infile.open("program2.txt", ios::in);

 if (!Infile)
 {
	cout<<"File not found!";
 }


 int i=0, h=0;

 while (ch = Infile.peek() != EOF)
  {
	Infile >> grade[i][h];
	i++;
	h++;
  }

  Infile.close();
}

/********************************************************/
int displaygrades()
{

fstream Infile;

for (int i=0; i < 15; i = i + 1) {
	for(int h=0; h < 6; h = h + 1){
	 Infile << grade[i][h] <<"\n";
	 }
	}
}

/********************************************************/

int main()
{
fileopen();
displaygrades(); 
return 0;
}


If anyone can help it would be greatly appreciated! THANKS!

Attachments Program2.cpp (0.84KB) program2.txt (0.45KB)
C++Newbie
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

What do you think is the loop condition in the following line?

while (ch = Infile.peek() != EOF)


[edit]Actually that whole loop is wrong.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I think Borland Turbo C++ has a peek() function to peek into memory, but not into files. It looks to me that you mean to use get() to get a character from the file? I am not quite sure what you want to do after that.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

you should anyway use ifstream instead of fstream for reading...

Omitting all errorchecking, the following will read and echo lines from a textfile:

#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
  ifstream fs;
  fs.open("test.txt");
  string s;
  while (fs >> s)  cout << s << endl;
  fs.close();
}
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

you should anyway use ifstream instead of fstream for reading...

Omitting all errorchecking, the following will read and echo lines from a textfile:

#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
  ifstream fs;
  fs.open("test.txt");
  string s;
  while (fs >> s)  cout << s << endl;
  fs.close();
}

Hey thanks alot for the help. That code works great. However, I need to manipulate that to work for a 2Dimensional array. If you have any idea as to how to do that please help. Thanks alot!

C++Newbie
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

I assume you have 1 row of the array in each line of your input file, and the records (so columns in that row) are separated in some way that you can detect.

What you do is you use that criterion to split up the string you read (it reads a line at a time in my code) and fill the columns of your array with the resulting data.

So in pseudo code:

while no readerror on read line
split line
set columns
increase counter
wend
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You