Link to home
Start Free TrialLog in
Avatar of zpivat
zpivat

asked on

A little help with indirecting?

Hi,

I'm having a little beginner's issue with indirection.

Suppose I have this file:
#include <stdio.h>

int main()
{
    int a;
    int b;
    scanf("%d", &a);

    printf("Enter a number: ");
    scanf("%d", &b);

    printf("Total is %d\n", a + b);
   
    return 0;
}

Suppose there's another file called input.txt, whose content is simply:
5



Assuming the executable is called prog, I'd run the program like this:
prog < input.txt

When I first tried it, I expected the program to read 5 from the file and get my own input from keyboard and then add their sum. Turned out it didn't work because I realised that it seems like once we use the < operator, the program will always assume that it is to get inputs from a file, and not from keyboard. My question is: is there anyway to terminate the file reading from a file (once it's done reading) and go back to the user input?
Or, if there's no way to do that, any other alternatives is welcome, as long as it can do what I want (i.e: read stuff from a file and then when it's done, get back to keyboard).

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FILE * infile;
char buffer[200];

infile = fopen ( "myfile.input", "r" );

while ( fgets ( buffer, 200, infile ) != NULL )
{
     printf ( "got %s from file",buffer);
}

fclose(infile);

add appropriate error checking
zpivat you have some old questions open

Questions Asked: 6
Questions Open: 5
Questions Graded: 1
Questions Deleted: 0
Last 10 Grades Given: A

take time to close them and reward the experts who helped you with your problems
Avatar of zpivat
zpivat

ASKER

sunnycoder,

Thanks for the help! Amazing!!

sorry if I didn't reward it. It's just that I'm fairly new to it and unused....
Of course I'll be more than happy to reward those who helped me :-)

Want to tell me how to get back to the old questions and reward those people?
hot to close questions:
https://www.experts-exchange.com/help/closing.jsp

to get to your questions, click on your name to reach your profile .. click question history to see a list of your questions ... close the ones which are marked unlocked
tried your
close(0)
open(stdin)
and it compiled fine, but then how come it gave:

Enter a number: Total is 4206633

because you still redirected input from file... it satisfied the read requests from the file... closing 0 and reopening it will not work as there are no more values to read !!!
Avatar of zpivat

ASKER

So where should I put the
close(0)
open(stdin)

then?
at a place where you will take some input from the user after opening stdin ... you can read in two sets of number instead of one and keep these statements in between the two sets of inputs
Avatar of zpivat

ASKER

Still can't get it to work....I tried this....just as you suggested, to put those two statements inbetween two sets of inputs...

#include <stdio.h>
#include <string.h>

int main()
{
    int a;
    int b;
    open(stdin);
    scanf("%d", &a);
   

    printf("Enter a number: ");
    scanf("%d", &b);
    close(0);    
    printf("Total is %d\n", a + b);

    return 0;
}
#include <stdio.h>
#include <string.h>

int main()
{
   int a;
   int b;

   scanf("%d", &a);
   printf("Enter a number: ");
   scanf("%d", &b);
   printf("Total is %d\n", a + b);

   close(0);    
   open(stdin);

   scanf("%d", &a);
   printf("Enter a number: ");
   scanf("%d", &b);
   printf("Total is %d\n", a + b);

   return 0;
}
Avatar of zpivat

ASKER

Thanks! Actually that's not quite what I want though. I want simply the program to read the integer from the file, then stop reading it and get the user input. What you gave me gave this result:
Enter a number: Total is 4206633
Enter a number: Total is 4206633

So I changed it (to suit what I want) and gave this:
Enter a number: Total is 4206633

#include <stdio.h>
#include <string.h>
int main()
{
    int a;
    int b;
    scanf("%d", &a);
    close(0);
    open(stdin);

    printf("Enter a number: ");
    scanf("%d", &b);

    printf("Total is %d\n", a + b);
   
    return 0;
}
>I want simply the program to read the integer from the file, then stop reading it and get the user input.
you mean one int from file and another int from user ?
Avatar of zpivat

ASKER

Yes, exactly! I want it to read the 5 from the input file (i.e: input.txt) and read another int from my own input from keyboard.
in that case, use this

fopen ( <your input file name> )
while ( fgets (...) != NULL )
{
     /* here you are reading values from file ... process them*/
}
fclose(..);

/*continue with input from keyboard*/