• C

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.
zpivatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
> 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
you are right

>is there anyway to terminate the file reading from a file (once it's done reading) and go back to the user input?
yes,
close(0);
open(stdin);

a better and cleaner way would be to do this

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

/*continue with input from keyboard*/

you just need to fill in arguments in above calls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunnycoderCommented:
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
0
sunnycoderCommented:
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
0
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

zpivatAuthor Commented:
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?
0
sunnycoderCommented:
hot to close questions:
http://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
0
sunnycoderCommented:
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 !!!
0
zpivatAuthor Commented:
So where should I put the
close(0)
open(stdin)

then?
0
sunnycoderCommented:
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
0
zpivatAuthor Commented:
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;
}
0
sunnycoderCommented:
#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;
}
0
zpivatAuthor Commented:
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;
}
0
sunnycoderCommented:
>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 ?
0
zpivatAuthor Commented:
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.
0
sunnycoderCommented:
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*/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.