By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Redirection in C

P: 1
im trying o write a program that takes in input from a textfile first, then allows the user to enter his own input later on in the program. Does anybody know how to transfer control of the keyboard back to the user in the middle of the program?

Here's my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #define MAX_CONTACT 100 // maximum number of contacts
  4. #define MAX_FIELDLEN 50 // maximum length of a field string
  5. #define MAX_LINELEN 250 // maimum length of a user-input line
  6. #define RETURN '\n'
  7.  
  8. char name[MAX_CONTACT][MAX_FIELDLEN];
  9. char telephone[MAX_CONTACT][MAX_FIELDLEN];
  10. char address[MAX_CONTACT][MAX_FIELDLEN];
  11. char city[MAX_CONTACT][MAX_FIELDLEN];
  12. char state[MAX_CONTACT][MAX_FIELDLEN];
  13. char zipcode[MAX_CONTACT][MAX_FIELDLEN];
  14. char email[MAX_CONTACT][MAX_FIELDLEN];
  15. char input[MAX_FIELDLEN];
  16. void exit();
  17. int read_line();
  18.  
  19. main(){
  20.     int count,character,file;
  21.     int i;
  22.     char c;
  23.     character=0;
  24.     file = 0;
  25.     while (( c = getchar()) != EOF){
  26.         ..... //Takes in the input from the file
  27.     }
  28.     while ((input[0] !='q') && (input[0] !='u') && (input[0] !='i') && (input[0] !='t')){
  29.         printf("\n\n-----Address Book-----\n");
  30.         printf("\n");
  31.         printf("----Search Record-----\n");
  32.         printf("----Insert Record-----\n");
  33.         printf("----Delete Record-----\n");
  34.         printf("--Review All Records--\n");
  35.         printf("---------Quit---------\n");
  36.         count=0;
  37.         while((c=getchar())!= RETURN){ --------------------------- I am having trouble here
  38.                                                                          input[count++]=c;------------------------------where the user is supposed to 
  39.                                                                                                                 }---------------------------------------------------------------------------- input data
  40.                                                                                                                      for (i=0;i<count;i++){
  41.                                                                                                                          printf("%c",input[count]);
  42.                                                                                                                      }
  43.         if (input[0]=='x'){
  44.             exit(0);
  45.         }
  46.         if (((input[0]=='S')||(input[0]=='s')) && (input[1]=='e') && (input[2]=='a') && (input[3]=='r') && (input[4]=='c') && (input[5]=='h')){
  47.             printf("You typed in search records");
  48.         }
  49.         else if (((input[0]=='I')||(input[0]=='i')) && (input[1]=='n') && (input[2]=='s') && (input[3]=='e') && (input[4]=='r') && (input[5]=='t')){
  50.             printf("You typed in insert records");
  51.         }
  52.         else if (((input[0]=='D')||(input[0]=='d')) && (input[1]=='e') && (input[2]=='l') && (input[3]=='e') && (input[4]=='t') && (input[5]=='e')){
  53.             printf("You typed in delete records");
  54.         }
  55.         else if (((input[0]=='R')||(input[0]=='r')) && (input[1]=='e') && (input[2]=='v') && (input[3]=='i') && (input[4]=='e') && (input[5]=='w')){
  56.             printf("You typed in review records");
  57.         }
  58.     }
  59.     printf("You typed in Quit");
  60. }
  61.  
Feb 21 '07 #1
Share this Question
Share on Google+
2 Replies


AdrianH
Expert 100+
P: 1,251
im trying o write a program that takes in input from a textfile first, then allows the user to enter his own input later on in the program. Does anybody know how to transfer control of the keyboard back to the user in the middle of the program?
[/code]
You can't as far as I know. When the redirection is complete, stdin is closed. However, you may be able to reopen it. This is a guess and I've not tried it, but use the standard C open() command. It will open a file handle that should replace the one that was just closed. It might work, this is the sort of thing to get pipes to work when forking. But its been a while for even that.

The one question remaining is, exactly what file do you open to get stdin from the user? Perhaps you can dup() the handle of stdin prior to reading from it. And then when stdin has closed, dup() the duped handle, which should replace the closed stdin.

What I am talking about is purely theoretical. Will it work? Give it a try and find out. :)


Adrian
Feb 21 '07 #2

AdrianH
Expert 100+
P: 1,251
Just for more info, you can go to http://www.gnu.org/software/libc/man...dLevel-I_002fO and read up on low-level IO.

Adrian
Feb 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.