Redirection in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackfrog
    New Member
    • Feb 2007
    • 1

    Redirection in C

    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:
    Code:
    #include <stdio.h>
    #define MAX_CONTACT 100 // maximum number of contacts
    #define MAX_FIELDLEN 50 // maximum length of a field string
    #define MAX_LINELEN 250 // maimum length of a user-input line
    #define RETURN '\n'
    
    char name[MAX_CONTACT][MAX_FIELDLEN];
    char telephone[MAX_CONTACT][MAX_FIELDLEN];
    char address[MAX_CONTACT][MAX_FIELDLEN];
    char city[MAX_CONTACT][MAX_FIELDLEN];
    char state[MAX_CONTACT][MAX_FIELDLEN];
    char zipcode[MAX_CONTACT][MAX_FIELDLEN];
    char email[MAX_CONTACT][MAX_FIELDLEN];
    char input[MAX_FIELDLEN];
    void exit();
    int read_line();
    
    main(){
    	int count,character,file;
    	int i;
    	char c;
    	character=0;
    	file = 0;
    	while (( c = getchar()) != EOF){
    		..... //Takes in the input from the file
    	}
    	while ((input[0] !='q') && (input[0] !='u') && (input[0] !='i') && (input[0] !='t')){
    		printf("\n\n-----Address Book-----\n");
    		printf("\n");
    		printf("----Search Record-----\n");
    		printf("----Insert Record-----\n");
    		printf("----Delete Record-----\n");
    		printf("--Review All Records--\n");
    		printf("---------Quit---------\n");
    		count=0;
    		while((c=getchar())!= RETURN){ --------------------------- I am having trouble here
    																		 input[count++]=c;------------------------------where the user is supposed to 
    																												}---------------------------------------------------------------------------- input data
    																													 for (i=0;i<count;i++){
    																														 printf("%c",input[count]);
    																													 }
    		if (input[0]=='x'){
    			exit(0);
    		}
    		if (((input[0]=='S')||(input[0]=='s')) && (input[1]=='e') && (input[2]=='a') && (input[3]=='r') && (input[4]=='c') && (input[5]=='h')){
    			printf("You typed in search records");
    		}
    		else if (((input[0]=='I')||(input[0]=='i')) && (input[1]=='n') && (input[2]=='s') && (input[3]=='e') && (input[4]=='r') && (input[5]=='t')){
    			printf("You typed in insert records");
    		}
    		else if (((input[0]=='D')||(input[0]=='d')) && (input[1]=='e') && (input[2]=='l') && (input[3]=='e') && (input[4]=='t') && (input[5]=='e')){
    			printf("You typed in delete records");
    		}
    		else if (((input[0]=='R')||(input[0]=='r')) && (input[1]=='e') && (input[2]=='v') && (input[3]=='i') && (input[4]=='e') && (input[5]=='w')){
    			printf("You typed in review records");
    		}
    	}
    	printf("You typed in Quit");
    }
    Last edited by AdrianH; Feb 21 '07, 01:05 AM. Reason: Reindented code and put code tags around it.
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by blackfrog
    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

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      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

      Comment

      Working...