#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    13

    Reading input from a file


    Hello all,
    I have a file that I need to read into my program at the command line. This program should take this input file (e.g. input1) and store it in two different array. This is how my code looks like but it doesn't work. Someone please help.
    This is what I would type in order to execute my program:
    Code:
    a.out < input1
    This is my input1 file:
    Code:
    1 2 3 4 5
    1 2 4 4 5
    This is my program:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    // Global values and arrays
    
    int preorder[26];
    int inorder[26];
    
    int main(int argc, char **argv){
      if (argc != 1){
        // report some error message
        cerr << "Error";
        exit(-1);
      }
      
      char *fileName = argv[2];
      
      cout << argv[2];
      
      // declared an ifstream object
      ifstream in(fileName);
      
      // if in cannot be opened, display error message
      if (!in){
        cerr << "File cannot be opened!\n";
        exit(-1);
      }
      
      // read information into array preorder[]
      int count = 0;
      while ((!(in >> "\n")) || count <= 26){
        cin >> preorder[count];
        cout << preorder[count];
        count++;
      }
    
      // read information into array inorder[]
      count = 0; 
      while ((!(in >> "\n")) || count <= 26){
        cin >> inorder[count];
        cout << inorder[count];
        count++;
      }
      
      return 0;
    }
    Someone please help. Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Inside the GNU/Hurd kernel
    Posts
    492
    Rep Power
    12
    Why are you doing a.out < input1?Shouldnt that be a.out input1?

    Second,what exactly doesn't work?

IMN logo majestic logo threadwatch logo seochat tools logo