Reading a character array from command line input file

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
20 40
........................................
........................................
...................o....................
....................o...................
..................ooo...................
........................................
........................................
........................................
........................................
........oo..............................
........oo..............................
..........oo............................
..........oo............ooo.............
.......................ooo..............
........................................
...........oo...........................
...oo.....o..o..........................
...oo......oo.........ooo...............
........................................
........................................


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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
 int main(int argc, char* argv[]){
    
    int iFilei = 2;

    int rows, col;
    char cell[rows][col];
    ifstream myiFile (argv[iFilei]);
    if (myiFile.is_open()){
      while (myiFile.good()){
        myiFile >> rows >> col;
        cout << "rows " << rows  << " columns " << col << endl;
          for (int i=0; i<rows; i++){
            for (int j=0; j<col; j++){
              myiFile >> cell[i][j];
              cout << cell[i][j];}}}
        myiFile.close();}
    else{
      cout << "Unable to open file" << endl;}

    for (int j=0;j<rows;j++){
      for (int i=0;i<col; i++){
       cout << cell[j][i] << " ";}
   }
   return 0;
}


I'd like to read in the array exactly how it looks, a 20x40 char array. When I run the program with:
./a.out -i cgl_example.dat

I get an output of:
rows 0 columns 4197019

I appreciate any help, thank you.
Try making a file called data.txt and fill it with the variables you want to read then replace
 
ifstream myiFile (argv[iFilei]);

with
 
ifstream myiFile ("data.txt");

and see if it works. If it doesn't then the problem is probably this line
 
ifstream myiFile (argv[iFilei]);

If it works then your problem is the rest of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;
}

is that what u wanted to do?
Last edited on
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?
We need to talk

-Ritu
1
2
    int rows, col;
    char cell[rows][col];
¿What is the size of the 'cell' matrix?

Don't loop on good() or eof(). Instead loop against the reading
while( input>>var ) that way you can be sure that it didn't fail.


@ui uiho: if you are going to threat the exception immediately, you could just use an if
ROFL you actually emailed the professor... bwaaahahahahahahaha
Topic archived. No new replies allowed.