User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,210 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,240 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2889 | Replies: 5
Reply
Join Date: Dec 2004
Posts: 2
Reputation: C++Newbie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
C++Newbie C++Newbie is offline Offline
Newbie Poster

General Protection Exception Error Please Help!

  #1  
Dec 1st, 2004
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!
Attached Files
File Type: cpp Program2.cpp (857 Bytes, 0 views)
File Type: txt program2.txt (464 Bytes, 0 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,422
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: General Protection Exception Error Please Help!

  #2  
Dec 1st, 2004
What do you think is the loop condition in the following line?
while (ch = Infile.peek() != EOF)
[edit]Actually that whole loop is wrong.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,337
Reputation: vegaseat is on a distinguished road 
Rep Power: 8
Solved Threads: 171
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: General Protection Exception Error Please Help!

  #3  
Dec 2nd, 2004
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.
May 'the Google' be with you!
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 187
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: General Protection Exception Error Please Help!

  #4  
Dec 3rd, 2004
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();
}
Reply With Quote  
Join Date: Dec 2004
Posts: 2
Reputation: C++Newbie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
C++Newbie C++Newbie is offline Offline
Newbie Poster

Re: General Protection Exception Error Please Help!

  #5  
Dec 3rd, 2004
Originally Posted by jwenting
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!
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 187
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: General Protection Exception Error Please Help!

  #6  
Dec 4th, 2004
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC