Solved

A little help with indirecting?

Posted on 2003-11-10
14
318 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:zpivat
  • 9
  • 5
14 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9714713
> 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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714750
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714776
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
 

Author Comment

by:zpivat
ID: 9715124
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9715133
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9720153
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
 

Author Comment

by:zpivat
ID: 9721592
So where should I put the
close(0)
open(stdin)

then?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9721617
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
 

Author Comment

by:zpivat
ID: 9723623
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9728407
#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
 

Author Comment

by:zpivat
ID: 9731183
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9731192
>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
 

Author Comment

by:zpivat
ID: 9731959
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737299
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now