944,209 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6186
  • C++ RSS
Dec 1st, 2004
0

General Protection Exception Error Please Help!

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <fstream.h>
  6. #include <ctype.h>
  7.  
  8. int grade[15][6];
  9.  
  10. /********************************************************/
  11. void fileopen()
  12. {
  13. char ch;
  14. fstream Infile;
  15.  
  16.  
  17. Infile.open("program2.txt", ios::in);
  18.  
  19. if (!Infile)
  20. {
  21. cout<<"File not found!";
  22. }
  23.  
  24.  
  25. int i=0, h=0;
  26.  
  27. while (ch = Infile.peek() != EOF)
  28. {
  29. Infile >> grade[i][h];
  30. i++;
  31. h++;
  32. }
  33.  
  34. Infile.close();
  35. }
  36.  
  37. /********************************************************/
  38. int displaygrades()
  39. {
  40.  
  41. fstream Infile;
  42.  
  43. for (int i=0; i < 15; i = i + 1) {
  44. for(int h=0; h < 6; h = h + 1){
  45. Infile << grade[i][h] <<"\n";
  46. }
  47. }
  48. }
  49.  
  50. /********************************************************/
  51.  
  52. int main()
  53. {
  54. fileopen();
  55. displaygrades();
  56. return 0;
  57. }

If anyone can help it would be greatly appreciated! THANKS!
Attached Files
File Type: cpp Program2.cpp (857 Bytes, 21 views)
File Type: txt program2.txt (464 Bytes, 21 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++Newbie is offline Offline
2 posts
since Dec 2004
Dec 1st, 2004
0

Re: General Protection Exception Error Please Help!

What do you think is the loop condition in the following line?
C++ Syntax (Toggle Plain Text)
  1. while (ch = Infile.peek() != EOF)
[edit]Actually that whole loop is wrong.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 2nd, 2004
0

Re: General Protection Exception Error Please Help!

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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 3rd, 2004
0

Re: General Protection Exception Error Please Help!

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

Omitting all errorchecking, the following will read and echo lines from a textfile:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. int main()
  7. {
  8. ifstream fs;
  9. fs.open("test.txt");
  10. string s;
  11. while (fs >> s) cout << s << endl;
  12. fs.close();
  13. }
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 3rd, 2004
0

Re: General Protection Exception Error Please Help!

Quote 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:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. int main()
  7. {
  8. ifstream fs;
  9. fs.open("test.txt");
  10. string s;
  11. while (fs >> s) cout << s << endl;
  12. fs.close();
  13. }
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++Newbie is offline Offline
2 posts
since Dec 2004
Dec 4th, 2004
0

Re: General Protection Exception Error Please Help!

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:
C++ Syntax (Toggle Plain Text)
  1. while no readerror on read line
  2. split line
  3. set columns
  4. increase counter
  5. wend
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with this conversion program
Next Thread in C++ Forum Timeline: C++ Data Types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC