Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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.
0
zpivat
Asked:
zpivat
  • 9
  • 5
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now