Ok, so I've checked several forums here for help but to no avail.
What I want to do is to read in a file name from the command line and then read from the file into a couple variables and then an array. It's part of a cellular automaton assignment actually. This is the input file:
The first two numbers in the input file are the number of rows and columns, which I have to read in as well. Here's my code so far, trying to read in the number of rows, columns, and then outputting the array to check I read it correctly (which I haven't):
#include <iostream>
#include <fstream>
#include <vector>
#include <exception>
int main(){
std::string temp;
std::vector<std::string> fileinfo; //strings and vectors
std::ifstream myiFile;//declare file as such
myiFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
myiFile.open("/Users/home/Desktop/test/test/testerfile.txt");//open file
} catch (std::ifstream::failure &e) {//catch an error
std::cout << "Error " << e.what() << " has occured";//helps for debugging
return 0;//end program if it does not open
}
for(int i = 0; myiFile.good() ; i++){myiFile >> temp; fileinfo.push_back(temp);}
//cin ignores whitespace will input 1 line at a time by default
myiFile.close(); //close the file
for(int i = 0; i < fileinfo.size(); i++){std::cout << fileinfo[i] << '\n';}
//prints file
return 0;
}
I would try that, but this is part of an assignment and the teacher will grade my program using cgl_example.dat from the command line as the input file. Is there a better way to read the input file from the command line than what I'm using?