Thread: Reading the input file into integer arrays

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Reading the input file into integer arrays

    Hey all,
    I have a question about file input processing. I need to read in a file along with my executable in UNIX like so:
    Code:
    prompt: a.out  < inputfile
    and I need to get two lines and store each of those lines into integer arrays for manipulation later. How would I go about doing that? Thanks.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    So your input file looks like this..

    Code:
    12 31 234 53 2 12
    234 2 2 212 3
    and you wan to read each row into different arrays?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Talking

    exactly! Would you know how to?
    Last edited by supaben34; 12-05-2002 at 02:09 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM