473,385 Members | 1,464 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,385 software developers and data experts.

reading stdin for key presses vs a redirected file

3
I'm trying to write a console program to exactly duplicate a much older dos-based program.

This older program uses getch() to prompt a user for a response to a question. This program is also used in batch files where a file containing just the response would be redirected into the program so it wouldn't wait for user input.

For example: program.exe < input.txt

It seems as though redirected files don't act the same as user input in a console program even though I expected them both to go through stdin. Getch() doesn't work with redirected files in a console program like in the above example.

I've had success using cin and also with CreateFile with CONIN$ as the input name, to handle keypresses as well as redirected files. However, when handling user keypresses, both alternatives require a carriage return before the input is processed, whereas the original program using getch() only required the keypress - no carriage return necessary.

Is there any Win32 equivalent to the getch() command or some workaround that would work with user keypresses as well as redirected input?

Thanks.
Taz
Mar 16 '10 #1
5 4364
weaknessforcats
9,208 Expert Mod 8TB
getch() reads a character directly from the keyboard.

getchar() reads a character from the console.

I suspect if you use getchar() instead of getch() then your problem with redirected input will disappear.
Mar 16 '10 #2
TazDev
3
Thank you for your suggestion. "getchar()" does work with both, but requires the user to hit return before processing.

I'm currently playing around with PeekNamedPipe in conjunction with CreateFile using CONIN$ to see if I can get something going.

If you have any other ideas, let me know!
Mar 16 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
I understand. But the enter key could be in your input file right?

Otherwise, you will ned to write the program to use an actual file.
Mar 16 '10 #4
TazDev
3
I meant when a user is in control, they still need to press enter. That's the main sticking point now.

It actually doesn't matter whether you have a carriage return in the file or not. It still gets processed.

I've got something that seems to work. It's not pretty but I can't figure out another way of doing this. Basically I check to see if there's anything in stdin. If there is I use whatever function I want to get it, otherwise I use getch().

Thanks for the help though.

Expand|Select|Wrap|Line Numbers
  1. BOOL check_stdin ()
  2. {
  3.     fpos_t pos;
  4.  
  5.     fgetpos (stdin, &pos);
  6.     ++pos;
  7.     if (fsetpos (stdin, &pos))
  8.         return FALSE;
  9.     else
  10.     {
  11.         rewind (stdin);
  12.         return TRUE;
  13.     }
  14. }
  15. main ()
  16. {
  17.     if (!check_stdin ())
  18.     {
  19.         // use getch ();
  20.     }
  21.     else
  22.     {
  23.         // use fgets or whatever
  24.     }
  25. }
  26.  
Mar 16 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
I meant when a user is in control, they still need to press enter. That's the main sticking point now.
That's the way it works. The user turns control back to the program by pressing enter.

There is no other way to tell of the user is done making input. Even then, when your program resumes, you have no idea what was entered. A robust input edit will be a very time consuming and complicated thing to write. I tell students to use "friendly" input. That is, enter what is expected. Use that to get the program working. When that's done you go back and add input edits. Don 't be surprised of the input edit code is larger than the program was in the first place.

Then there's other stuff, like EOF (the same as CTRL+Z) which indicates end-of-all-input. When the user enters CTRL+Z and you pick up on that to mean all input is done, there is still the enter key used to enter CTRL+Z in stdin waiting top trip you up.

I still an uncertain why a file can't work for you. When you press enter to record a line in the file, that enter will be fetched just like the user pressed the key.

Your only other option is to write your own keyboard driver functions and use them instead of the standard ones supplied with C. That's what Windows does.
Mar 17 '10 #6

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

Similar topics

2
by: Ian Pawson | last post by:
I have php 4.3.3 on my local IIS5.1 server (XP) in ISAPI mode. It all works ok except that it does not seem to be reading the php.ini file as any changes I make are not shown up when doing a...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
6
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
11
by: Michael McGarry | last post by:
Hi, I am reading strings from a text file using fscanf("%s", currToken); This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the...
2
by: SteMc | last post by:
today I tackled, for the first time, opening and reading from a text file. Following the example on the MSDN and declared a variable, strline as a string and objstreamreader as a streamreader. ...
0
by: PRITPAL | last post by:
Hi There, I want a code for Saving and Reading formatted text (RTF File) in MS Access using ole Objects, i want to save 20 such records in DB using VB 6.0. Plz Help
1
by: vuyyurus | last post by:
Need help in reading a different word file which has table with rows and columns and write to different word document in certain table with rows and cloumns.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.