2 years ago
#73012
Gunslinger56
How to get input file to open?
For this project, we are supposed to use an input and output file to better organize our data. Howvever, for whatever reason, every time I rube the program it says
"File couldn't open. Terminating.
Process finished with exit code 1"
I have tried several different methods to try and get the program to open the file (using different commands, changing filename, etc), however each time it gives me the above statement.
Here is the code for my program (please note there is much more code than this. However, this is the only area that uses the input file, so I am confident that the error is residing somewhere withing here):
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <limits>
#ifdef _MSC_VER // Memory leak check
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#else
#define VS_MEM_CHECK
#endif
using namespace std;
const int NUM_GRADES = 5;
int main(int argc, char* argv[]) {
VS_MEM_CHECK // Enable memory leak check
ifstream inputFile; // Reading Input file
inputFile.open(argv[1]);
if (inputFile.is_open()) { // Checks if file opened succesfully
cout << "Input File opened successfully.\n";
} else {
cout << "File couldn't open. Terminating.\n";
return 1;
}
int numStudents;
int numExams;
inputFile >> numStudents >> numExams;
inputFile.ignore(std::numeric_limits<int>::max(), '\n');
string *arrayNames = new string[numStudents]; // Intializes Students Array
double *arrayTotalGrade = new double[numStudents]; // Initializes Total Score Array
double *arrayAverages = new double[numExams]; // Initializes Average Score Array
int **arrayScores = new int *[numStudents]; // Initializes Scores Array
for (int i = 0; i < numStudents; ++i) {
arrayScores[i] = new int[numExams];
}
int **arrayGradeCount = new int *[numExams]; // Initializes Grade Count Array
for (int i = 0; i < numExams; ++i) {
arrayGradeCount[i] = new int[NUM_GRADES];
for (int j = 0; j < NUM_GRADES; ++j) {
arrayGradeCount[i][j] = 0;
}
}
for (int i = 0; i < numStudents; ++i) {
string line;
string name;
getline(inputFile, line);
size_t p = 0;
while (!isdigit(line[p])) ++p; // line[p] is the location of the first digit on the line
name = line.substr(0, p - 1); // Gets name from file, p-1 removes an extra whitespace.
arrayNames[i] = name;
line = line.substr(p); // Isolates scores on line
istringstream iss(line); // Puts line (now with only scores' values) into an istringstream
for (int j = 0; j < numExams; ++j) // Puts scores from istringstream into arrayScores onto row 'i'
{
int scores;
iss >> scores;
arrayScores[i][j] = scores;
}
}
inputFile.close();
}
c++
input
file-io
0 Answers
Your Answer