•
•
•
•
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
![]() |
•
•
Join Date: Dec 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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:
If anyone can help it would be greatly appreciated! THANKS!
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? [edit]Actually that whole loop is wrong.
while (ch = Infile.peek() != EOF)
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,337
Reputation:
Rep Power: 8
Solved Threads: 171
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation:
Rep Power: 18
Solved Threads: 187
you should anyway use ifstream instead of fstream for reading...
Omitting all errorchecking, the following will read and echo lines from a textfile:
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();
}•
•
Join Date: Dec 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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!
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation:
Rep Power: 18
Solved Threads: 187
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:
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- general protection error (C++)
- Help!!!!!! General Protection Exception (C++)
- IEXPLORE caused a general protection fault (Web Browsers)
- I keep getting an error message on my computer called General Protection Exception... (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with this conversion program
- Next Thread: C++ Data Types



Linear Mode