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!

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.