tags:

views:

166

answers:

6

My program reads a list of integers from user input [ keyboard] and calculates some statistics

The user enters 'x' to terminate the input process.

So for example,

Enter integers separated by space ( enter x to quit) : 1 2 3 4 5 x

But now I want to include the inputs to be read from file redirection also. So if the numbers followed by x is in a data file, the program should take it from there if not then prompt the user

+1  A: 

use isatty for your file descriptor (0 for standard input)

example:

#include <unistd.h>
main(){
  if(isatty(0))
    puts("tty");  // print some prompt
  else
    puts("pipe"); // not really needed in your case
}
catwalk
A: 

You can read strings from a file with fstream and iostream.

Then from there you just parse the strings to see if they contain the correct data. If not, prompt the user for input?

PherricOxide
A: 

If what you mean is that you can either call your program with a file as a command line argument, or not, in which case you want it to read from standard input, you can do something like this:

#include <fstream>
#include <iostream>

void run(std::istream& in) {
   // read all input from 'in' and run your program as normal
}

int main(int argc, char **argv) {
   if(argc == 1) {
      run(std::cin);
   } else if(argc == 2) {
      std::ifstream fin(argv[1]);
      run(fin);
   }

   return 0;
}

This way, if you call

./prog

it'll read from standard input (i.e., the keyboard), and if you call

./prog foo.txt

it'll read from the file foo.txt.

You may want to do a little more work when checking the command line arguments, but this is the basic idea.

Jesse Beder
A: 

one approach that would not require any program alterations is to let the shell do the redirection for you.

On both Windows and Unix shells, < redirects a file to stdin for the program.

So, on unix/linux/mac on a console:

./app < file.txt

or windows, at the command prompt, just:

app < file.txt

will take the contents of file.txt and send it as stdin to the program called 'app'.

Will
A: 

On a unix box the following should work:

progream <file

where program is your program and file contain the input that the user would type in.

David Harris
A: 

A method that I use is to put the input routine into a function that uses istream. Call the function using std::cin or from a file.

Example:

#include <iostream>
#include <cstdlib>

bool
Get_Input_Value(std::istream& inp,
                int&  value)
{
  inp >> value;
  return inp.good();
}

int
main(void)
{
  std::cout << "Enter numbers: ";
  std::cout.flush() // Make the prompt appear.
  int the_value = 0;
  bool success = false;
  success = Get_Input_Value(std::cin, the_value);
  if (success)
  {
    std::cout << "\nThe value entered: " << the_value << std::endl;
  }
  else
  {
    std::cerr << "An error occurred with the input stream.\n";
  }
  return EXIT_SUCCESS;
}

The concept here is to use an std::istream variable for your input and assign it to a file or std::cin before any processing.

Thomas Matthews
To use the special code formatting, precede each line with four spaces, or select the region and hit the icon that looks like 1's and 0's.
luke