473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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:
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
2 2162
AdrianH
1,251 Expert 1GB
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
1,251 Expert 1GB
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

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

Similar topics

2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
52
by: Gerard M Foley | last post by:
Can one write a webpage which is not displayed but which simply redirects the user to another page without any action by the user? Sorry if this is simple, but I am sometimes simple myself. ...
15
by: Taki Jeden | last post by:
Hello everybody Does anybody know why w3c validator can not get pages that use 404 htaccess redirection? I set up two web sites so that clients request non-existent urls, but htaccess redirects...
2
by: Nadav | last post by:
Hi, Introduction: *************** I am trying to redirect stdout to a RichEdit control, this is done by initiating a StringWriter, associated it with a StringBuilder and setting the...
0
by: Dimitrios Mpougas | last post by:
Hello, I have two asp.net pages. The first is a page (main.aspx) wich has four links on it. The href value of each link is: href="view.aspx?id=1" traget="_blank" href="view.aspx?id=2"...
8
by: Luciano A. Ferrer | last post by:
Hi! I was following the http://www.seomoz.org/articles/301-redirects.php article, trying to do that with one of my test sites I added this to the .htaccess file: RewriteEngine On RewriteCond...
13
by: souissipro | last post by:
Hi, I have written a C program that does some of the functionalities mentionned in my previous topic posted some days ago. This shell should: 1- execute input commands from standard input,...
1
by: comp.lang.php | last post by:
require_once("/users/ppowell/web/php_global_vars.php"); if ($_GET) { // INITIALIZE VARS $fileID = @fopen("$userPath/xml/redirect.xml", 'r'); $stuff = @fread($fileID,...
4
by: psual | last post by:
hi newbie in web design I got some issue concerning a secure redirection between 2 pages let say I have a page with a grid (the 'master' page) in this grid I can select a record, get its pk...
13
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.