Skip to main content
gamedev.net
🔒 Locked

C++ input streams

Started by PhiberOptic 4 replies 1.2k views
Original Post
PhiberOptic
PhiberOptic
Hi there. Is there a chance to see what kind of input source I have on my inputstream in a regular c++ win32 console applikation? Like where do the input from cin come from. Mostly it's from the keyboard, but say, if you want to check if the user typed: "program.exe < file.txt" Is there a chance to see that under windows? I know how to do it on unix systems.. thanx
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
taby
taby
This will spit out a redirected file, ie: program.exe < file.txt

#include <iostream>using std::cin;using std::cout;using std::endl;#include <string>using std::string;using std::getline;int main(void){	string line_input;	while(getline(cin, line_input))		cout << line_input << endl;	return 0;}
PhiberOptic
PhiberOptic
Yeah, I know that. But I want to know if there is a file, or if the input comes from your keyboard.
How do I do that with a Win32 system?
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Drew_Benton
Drew_Benton
Quote:
Original post by PhiberOptic
Mostly it's from the keyboard, but say, if you want to check if the user typed: "program.exe < file.txt"


If you want to check what the user has typed, you can modify PhiberOptic's code some:
#include <iostream>#include <string>int main(void){	// String for the input	std::string line_input;	// Get one line of input	std::getline( std::cin, line_input );	// See if the "<" character is found	int mPos = line_input.find( "<" );	// If so, then you have your command	if( mPos != -1 )	{		// Get the first token		std::string token1 = line_input.substr( 0, mPos );		// Get the second token (added 2 rather than the normal 1 because of the extra space )		std::string token2 = line_input.substr( mPos + 2 );		// Show us the tokens		std::cout << "Token1: " << token1 <<  std::endl;		std::cout << "Token2: " << token2 <<  std::endl;                // Do w/e you want with them here	}	return 0;}


When the user enters in "program.exe < file.txt" you will get "program.exe" as token1 and "file.txt" as token2. Using those filenames you can then add whatever handling code you want. If you want to open those files, you will need to use the fstream class.

Basically what you want to do is get your input, then you parse though it. That example of parsing is trivial, there are a lot of better ways to do it. If this is not what you are looking for you will have to explain what you are looking for. Perhaps show how it's done in Linux so we know what you are looking for.
Fruny
Fruny
Drew_Benton - I hate to be the one to break it to you, but

a) He's talking about argc/argv stuff. How your program was started, not in put you fed it.
b) Shell redirections are not part of the command line as seen by your program anyway.


PhiberOptic - As far as I can tell by skimming the MSDN docs, you'd first convert the fd to a HANDLE with _get_osfhandle(), and then use some Win32 functions (google cache, the page on MSDN is 404) to extract the file name. I don't know how it represents terminals, though, nor if there's an equivalent to isatty() - start looking here.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
PhiberOptic
PhiberOptic
Thank you Fruny! At least one who understod what I meant :)
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.