4

So we are learning about scanf and redirection of input. I have to read in the data from stdin and store it in an array of a predetermined size. Afterwards we have to ask the user for an integer x and do some basic searching for that integer in the array. The user at the command line must use "./a.out< somefile.txt" to provide the data. I am reading this data via scanf and I have used a while loop that terminates when scanf reads EOF. The problem is when I want to re-use scanf to ask for user to enter the integer x to search for in the array. scanf always returns -1 and stores 0 in x, so I can't seem to use scanf. I am confused by the activity of scanf. Below I provide a simple code of my program and a sample .txt file.

data.txt file that user enters at command line via redirection "<"

1
2
3
4
5
6

Now my sample program

int main(){

   int arr[6], i = 0, value, x;

   while(scanf("%d",&value) != EOF){
         arr[i] = value;
         i++;
    }

    printf("Enter integer to search for: ");
    scanf("%i", &x);
    printf("You entered %i", x);
    return 0;
}

Now this is the output, it just runs and does not wait for user to input data

Enter integer to search for:
You entered 0
1
  • @haccks: You might need a newline at the end of your data.txt. Commented Jan 30, 2014 at 19:11

3 Answers 3

3

The problem you're having is that standard in is the only place you're getting input from, and since it's being redirected, it's no longer connected to your terminal. You'll want to take a look at fscanf and use /dev/tty instead of trying to scan from standard in to get interactive user input.

You might also want to look at scanf's return value to make sure it's succeeding.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok it seems I might have misunderstood the project assignment. The user does not have to redirect input, but instead just type in the data values one by one till a certain integer is entered. I want to thank you for the reply, I now understand more about redirection, and your explanation makes sense.
0

Quoting from http://c-faq.com/stdio/devtty.html:

Q: I'm trying to write a program like "more." How can I get back to the interactive keyboard if stdin is redirected?

A: There is no portable way of doing this. Under Unix, you can open the special file /dev/tty. Under MS-DOS, you can try opening the "file" CON, or use routines or BIOS calls such as getch which may go to the keyboard whether or not input is redirected.

I've done a quick check of this for one version of Unix. If foo.c is a modified version of your sample program:

#include <stdio.h>

#define NUMBER(x) ((int)(sizeof(x) / sizeof(x)[0]))

int main(void) {
    int arr[6], i = 0, value, x;
    while (i < NUMBER(arr) && scanf("%d", &value) == 1)
        arr[i++] = value;
    printf("Have read in %d values\n", i);

    if (!freopen("/dev/tty", "r", stdin)) {
        perror("freopen");
        return -1;
    }
    printf("Enter integer to search for: ");
    scanf("%i", &x);
    printf("You entered %i\n", x);
    return 0;
}

Then on RHEL 5, with both gnome-terminal and xterm:

$ cat test.txt
1
2
3
$ foo < test.txt
Have read in 3 values
Enter integer to search for: 99
You entered 99

Comments

0

You're using the return value of scanf and checking it against EOF. The return value of scanf is the number of variables successfully read, or something like that, NOT the value that was assigned IN scanf. You need to check value against EOF.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.