2 Replies - 4585 Views - Last Post: 14 February 2012 - 05:22 AM Rate Topic: -----

#1 jingoria   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

C type I/O redirection in C++

Posted 13 February 2012 - 09:46 PM

My assignment requires me to get input using I/O redirection like:

I tried looking this up on google but I see this as syntax for C.

But how do I use this for C++? I tried implementing it with copy pasting the statement as it is but I keep getting compile errors (which is pretty obvious).

I was wondering since I was using DevC++ as my compiler/IDE and if that could be a cause of not identifying C syntax and if I should try some other way to compile it.

If I should try other way to compile, then please let me know how.


Is This A Good Question/Topic? 0
  • +

Replies To: C type I/O redirection in C++

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2284
  • View blog
  • Posts: 4,375
  • Joined: 30-May 10

Re: C type I/O redirection in C++

Posted 13 February 2012 - 10:17 PM

Redirection happens on the command line - you don't change anything within the code at all.

$ cat bar.c
int main()
  char buff[BUFSIZ];
  int count = 0;
  while ( fgets(buff,sizeof buff, stdin) != NULL ) {
  printf("%d lines entered\n",count);
  return 0;

Now run the code in several different ways
$ # read from console
$ ./a.out 
the quick brown fox
jumps over the
lazy dog
3 lines entered
$ # read from redirection
$ ./a.out < bar.c
11 lines entered
$ # read from pipe
$ cat bar.c | ./a.out 
11 lines entered

How you do this from within an IDE depends on how sophisticated your IDE is.
At a basic level, you can set command line parameters, but whether that goes as understanding shell meta characters like < is another matter.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon

Reputation: 7361
  • View blog
  • Posts: 15,285
  • Joined: 16-October 07

Re: C type I/O redirection in C++

Posted 14 February 2012 - 05:22 AM

Streams are streams are streams. In C you'll sometimes talk about file handles, but it's still streams. In C++, streams.

Here's a quick example:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void process(istream &in) {
	int count = 0;
	string buff, line;
	while(getline(in, buff)) { 
		line = buff;
	cout << count << " lines processed, last line: " << line << endl;

int main(int argc, char *argv[]) {
	return 0;

Here's some output:
[email protected]$ g++ streamTest.cc
[email protected]$ ./a.out 
2 lines processed, last line: bar
[email protected]$ ls | ./a.out 
394 lines processed, last line: xlib.pdf
[email protected]$ ./a.out < streamTest.cc
27 lines processed, last line: 
[email protected]$

Note that for keyboard entry, I had to use a ctrl-D, which is EOF on my OS. A ctrl-C would abort the program and give me nothing. However, output from a program or a piped file come with their own EOF.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1