Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

UNIX file redirection (programming for)

UNIX file redirection (programming for)

UNIX file redirection (programming for)

Hello everyone,
   I need to know how to code a program so that it will accept redirection.  This program is supposed to accept a list of numbers and store them in variables for later use in the program.  When putting numbers in on the command line as arguments, it works fine... but as soon as I type this on the command line -

myprog.exe < data.txt

- I get a stack dump.  data.txt contains numbers separated by spaces and line feeds, and myprog.exe is the compiled prog. I use argc and argv[], and I run into stack dumps with the redirection.  

I need to make my program understand how to accept input from redirection.  And the catch here is - *without* using file functions, using only redirection.

Maybe if someone has a simple and short sample code laying around similar to this, could you post it?

Any comments, ideas, or anything else are welcome.


RE: UNIX file redirection (programming for)

#include <stdio.h>

int main () {
  int x, i = 1;
  while (scanf("%d", &x) != EOF)
    printf ("%d: %d\n", i++, x);

RE: UNIX file redirection (programming for)

Perhaps following sentence may work

cat data.txt|myprog.exe


RE: UNIX file redirection (programming for)

Ok thanks, very good info...

   Does scanf() work if I want to detect line feeds?  Or does it strip them out?  What would I use to detect line feeds?


RE: UNIX file redirection (programming for)

#include <stdio.h>

int main () {
  int x, lin = 0, col = 0;
  char tmp_ch;
  while (scanf("%d", &x) != EOF) {
    printf ("(lin=%d,col=%d): %d\n", lin, col++, x);
    scanf ("%*[ \t\r]"); /* skipping whitespaces and CRs */
    /* unfortunately scanf doesn't count void (that is %*) conversions,
     so scanf ("%*1[\n]") is always 0. */
    while (scanf ("%1[\n]", &tmp_ch) == 1) { /* instead you could use getc and ungetc */
      lin++; col=0;
      scanf ("%*[ \t\r]");
  printf ("Number of lines: %d\n", lin);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close