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
#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;
}
1234	 052	 007	 100	 078	 034
2134	 090	 036	 090	 077	 030
3124	 100	 045	 020	 090	 070
4532	 011	 017	 081	 032	 077
5678	 020	 012	 045	 078	 034
6134	 034	 080	 055	 078	 045
7874	 060	 100	 056	 078	 078
8026	 070	 010	 066	 078	 056
9893	 034	 009	 077	 078	 020
1947	 045	 040	 088	 078	 055
2877	 055	 050	 099	 078	 080
3189	 022	 070	 100	 078	 077
4602	 089	 050	 091	 078	 060	
5405	 011	 011	 000	 078	 010
6999	 000	 098	 089	 078	 020

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

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

[edit]Actually that whole loop is wrong.

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.

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();
}

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!

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
This question has already been answered. Start a new discussion instead.