C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2002, 07:01 PM
supaben34 supaben34 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Tallahassee
Posts: 55 supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 12
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.

Reply With Quote
  #2  
Old December 7th, 2002, 08:44 AM
Tuxie Tuxie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Inside the GNU/Hurd kernel
Posts: 492 Tuxie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m
Reputation Power: 12
Why are you doing a.out < input1?Shouldnt that be a.out input1?

Second,what exactly doesn't work?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading input from a file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap