C++ :: Restore Keyboard Stdin After Reading From Pipe?
Apr 25, 2014
I use the following code segment to read and output the text piped to a program. If there is no piped text, the code segment is skipped and then the program continues. What I need to do is restore stdin to the keyboard after reading from the pipe. I use the PeekNamedPipe() and ReadFile() because reading from stdin blocks until it has something, and I don't want that to happen. After reading from the pipe, the program begins to execute, and the main loop can be paused and it prompts for a command. I just can't figure out how to restore input to the keyboard. Obviously the platform is Windows.
char char_buffer[1024];
DWORD bytes_read;
DWORD bytes_avail;
DWORD dw;
HANDLE stdin_handle;
bool is_pipe = false;
[code]....
View 4 Replies
ADVERTISEMENT
Oct 31, 2014
Why I have an infinite loop when reading from a pipe?
i = 0;
while(i < 10) {
test_value = read(pipe3[READING], &message, sizeof(struct MESSAGE));
printf("test_value is %d
", test_value);
//printf("Entering infinite loop
[code]....
View 10 Replies
View Related
Mar 26, 2014
I am aiming to read an integer from stdin(pointed to the uart) and echo it with the uart interrupt service routine. there is a simple retarget file along with the main code shown below. So far i can read chars (char x[32] but i am struggling with int.
I have gathered that i need to use the scanf function to read an int from the pointer defined in fgets.
My output is giving me weird values, i enter 8 and ill get a random 3 digits back. I have a feeling its a problem with the input buffer.
Code:
//------------------------------------------------------------------------------// Cortex-M0//------------------------------------------------------------------------------
#include <stdio.h>#include <time.h>#include <rt_misc.h>
#include <stdlib.h>#define AHB_LED_BASE
0x50000000#define AHB_UART_BASE
0x51000000void UART_ISR(){
[Code] .....
View 9 Replies
View Related
Sep 12, 2014
I thought maybe there was something in C that could read full sentences from stdin
Code:
scanf("%100[^
]s", string);
But that's not working for me. so i came up with my own function and its not giving me the results i want. here is the function including the call from main:
Code:
/* * * * * * * * * * * * * *
* FROM MAIN *
* * * * * * * * * * * * * */
printf("
Adding a new part...
");
printf("Enter part name: ");
get_string(new_part.pname);
[Code] .....
/* * * * * * * * * * * * * * * * * * * * * * *
* UNDESIRED OUTPUT *
* * * * * * * * * * * * * * * * * * * * * * */
8newpart
Where is the eight coming from? i thought fpurge clear the buffer. Also, I'm trying to add spaces in between words... i thought maybe putting within the while loop but outside of the if statement string[length +1] = '' would work, but it doesn't. so i put it outside of the loop but that i knew that wouldnt work either.
Problem #2 is reading from a file.. so far i have the following code which reads everything perfectly except the .txt file has a new line character at the end and i think its reading it:
Code:
/* * * * * * * * * * * * * * * * *
* READS FROM FILE *
* * * * * * * * * * * * * * * * */
if(read_in != NULL)
{
while ((fgets(read_string, MAX_PARTS ,read_in) != NULL) && (array_position < MAX_PARTS))
[Code] ....
0 in stock i want it to stop after reading the ball bearings line. a lot of issues for one post, but all related to reading inputs so i put it all on one.
View 2 Replies
View Related
Mar 8, 2013
I'm writing a program that reads from stdin and prints it back out. I'm getting a bunch of garbage then a seg fault.
Code:
double numbers[256];
memset (numbers, 0, sizeof (numbers));
for (;;) {
double num = scanf ("%255lg", numbers);
if (num== EOF)
break;
}
for (int i = 0; i < (int)sizeof (numbers); ++i) {
printf ("%g
", numbers[i]);
}
View 3 Replies
View Related
May 10, 2014
I have an array of groups of numbers (I call them classes), that might look like this:
Code: static const int arr[] = {1,1,3,3};
std::vector<int> classes( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
or like this
Code: static const int arr[] = {1,3,3,3,5,5,6,8};
std::vector<int> classes( arr , arr + sizeof(arr) / sizeof( arr[ 0 ] ) );
The groups of numbers are in an increasing order, but not numbered consecutively. So, would I would like to have is this
1,1,2,2
and
1,2,2,2,3,3,4,5
A loop like this
Code: for ( int i = 1; i < classes.size(); i++ )
if ( classes.at( i ) > classes.at( i - 1 ) )
classes.at( i ) = classes.at( i - 1 ) + 1;
does not work, because it changes the preceding value in the group, thus destroying the group. How would you do it?
View 9 Replies
View Related
Nov 9, 2012
My 'c' program reads each line of a .txt file, makes a few changes and then outputs the line of text (which are names). I want the contents to be sorted and did not want to bother writing my own sort. The program runs in a command prompt from inside a batch file for example:
sort | makelist.exe BluRay1.txt > sorted.txt
sort | makelist.exe Dvd1.txt >> sorted.txt
..etc..
I have to supply a ctrl-z manually to close the piping. I tried the following which did not work.
buffer[0] = 26;
printf("%c",buffer[0]);
//printf("^(z)");
fclose(stdout);
fclose(stdin);
I never got the above to work so I am avoiding piping and just do a redirect instead.
makelist.exe dvd1.txt > unsorted.txt
makelist.exe dvd2.txt >> unsorted.txt
sort < unsorted.txt > sorted.txt
..etc.
Just wondering if I could have gotten the ctrl-z thru the pipe using "C".
View 2 Replies
View Related
Oct 30, 2014
I have this if block that is supposed to be creating a pipe then forking and I would like to combine the while loop below it with it. How would I do that?
p = pipe(pipe1);
if (p < 0) {
printf("pipe error");
exit(0);
} else {
printf("successful pipe1 = %d",p);
[Code].....
I have trying to read this documentation but don't understand it. [URL] ....
View 1 Replies
View Related
May 30, 2014
I have study that create a pipe and set a terminal of this pipe like a stdout of a process is a way to implement inter process comunication, but how can i do it in c++?
View 2 Replies
View Related
Mar 7, 2014
i tried to parse the string data seperated by Pipe('|') delimiter, here i am getting some error.Please find the below code.
[char* getData(){
char* string = "1355|||250|New";
char* tok1[10],tok2[10],tok3[10],tok4[10],tok5[10];
sscanf(string,"%[^'|'],%[^'|'],%[^'|'],%['^|'],%s",tok1,tok2,tok3,tok4,tok5);
printf("%s %s %s %s %s",tok1,tok2,tok3,tok4,tok5);
}]
I want to print the Value 250 in my string, but it was displaying some garbage values.
View 1 Replies
View Related
May 23, 2013
Is there a maximum capacity for stdin ?
View 2 Replies
View Related
Oct 6, 2014
Im writing a scientific software where I like to sent a 2D array (5x4) over a named pipe from a server to a client. When im sending a static array (i.e., double res[5][4];), all goes fine and it works perfect, but when I allocate a dynamic array, it provides some nonsense numbers at the client side. I feel it might be caused because I point to a memory that cannot be shared through a pipe. Am I right and how can I pass the dynamic allocated array itself over the pipe.
//Server program
// Create a pipe to send/receive data
HANDLE pipe = CreateNamedPipe(
"\.pipemy_pipe", // name of the pipe
PIPE_ACCESS_DUPLEX, // 2-way pipe -- send and read
PIPE_TYPE_BYTE, // send data as a byte stream
1, // only allow 1 instance of this pipe
0, // no outbound buffer
[Code] .....
View 2 Replies
View Related
Feb 19, 2015
I'm learning internet sockets right now and to that end I've made a simple client/server chat program centered around select(). I've got it to where multiple messages can be sent and received on either side and the "prompt" will move down 1 line each time accordingly.
My only sticking point is when someone is in the middle of typing a message and a new message is received. The message they are currently typing is going to be deleted, so they'll have to start over again. What I want to do is grab the current contents of the stdin buffer (meaning, there's no ), save it, print the received message and move the prompt downward as usual, and then put that saved message back into the buffer, meaning not only is it back on the screen now, it's erasable too as if nothing ever happened.
I know that this will definitely be some very very non-standard C, and that's fine. To that end, I've read that curses, GNU readline, and termios are possibilities for this. I've tried those, but am having trouble making it work.
This will be a moot point when I put a GUI on it soon (probably wx, but maybe Qt) since it won't even be an issue, but I'm determined to make this work. Both systems (the "client" and the "server") are Linux, one being Ubuntu and one being Debian.
View 2 Replies
View Related
Mar 23, 2014
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_READ 2
int main( ) {
char * secret = "abecedarium consisting This is a string literal!";
[Code] ....
I am trying to test what happens in the sscanf converted types (command and string) if they are smaller, i got surprised that the printf worked, so i dont know why.
this code is just a test for what i want to do, what i really want to do is for example: i have this string from the stdin which must have the following format:
connect name.surname
So I need to check the cases when it is not the input that i want, namely if the name.surname is for example bigger than 30 chars. and also if it has the abecedary characters from the ascii...
View 2 Replies
View Related
Dec 9, 2014
I'm trying to write to a named pipe created by a service, as we all know the session 0 isolation implemented in vista and forward makes this task a bit complicated.
well at this point i managed to make almost all to work but my real problem comes when i try to write on the named pipe from my GUI application with no administrator rights
If i run the GUI application with admin rights it works 100% but, I don't need that application to require the user admin rights and for security reasons i rather to leave it without admin...
so i started my research and i found that there is a way to achieve this by calling CreateNamedPipe() with a low integrity security attributes...
well how to implement but i finally made it, the problem is that it gets worse than passing null security attributes, it works with admin rights with NULL security attributes, but when i pass the low integrity security attributes it gives "access denied" even when using admin rights, so i guess im passing the wrong security attributes but how to manually create the security descriptor string.
This is the code:
Service (session0) SERVER
Code:
DWORD WINAPI PipeThreadRSVS(void* pParameter){
LPTSTR _PIPE_NAME = "\.pipeRSVHPipeIn";
bool Break=false;
char Received_Buffer[BlockSize+16];
DWORD BytesRead = 0;
[Code] ....
View 2 Replies
View Related
Sep 14, 2014
Is it possible to create a dynamic char array on the fly directly from stdin? I do not want to create a fixed length array before hand and then copy contents of it into a malloc created array.
Code: //[1]
char line[MAX1];
gets(line);
[Code]....
I could do either [1](buffer overflow problem) or [2] and then goto [3]. But both will have a problem if the input is more than the size MAX1(use defined).
Is it possible to do something of the effect of readLine() method of BufferedReader class in Java or the Console.readLine in .NET? Is it possible to peek into stdin and see the size of the input and then creat an array of the same exact size?
View 8 Replies
View Related
Oct 10, 2014
I want to read a string of unknown length from stdin. I tried to follow the approach from this link. URL....My code is like this:
#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
}
[code]......
What I have noticed is that if I take integer input from cin (cin >> n;) in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.What is the best way to read from console multiple strings of unknown length in combination with the integers?
View 5 Replies
View Related
Mar 20, 2014
I have a pre-declared array which sorts strings to it's alphabetic order and want to change it so it reads from stdin.
char *array[] = {"aaa", "ccc", "bbb", "ddd"}
I tried doing something like this:
for (i = 0; i < length; i++)
scanf("%s", &array[i]);
I just can't bring it to work. Another thing is, the input is a a bunch of strings separated by commas and ends with a period. Since I have to make a working C model which gets translated to assembly language later on I can't use functions like strtok.
View 6 Replies
View Related
Nov 10, 2013
I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below
rhhrkkj$hghjhdf$ddfkrjt
While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.
Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.
View 6 Replies
View Related
Jun 11, 2013
I want to intercept key to my program even if it is non active. I'am working on linux machine(ubuntu)..what should i use to do this?
View 6 Replies
View Related
Mar 6, 2015
What C command responds to a keyboard hit? I want to exit in an orderly manner from a continuous loop when a key is pressed.
View 5 Replies
View Related
May 7, 2014
Here is the function I am trying to modify...
Code:
void DisplayAdminMenu() {
char ch;
system("cls");
printf("Admin Menu");
[Code] ......
Error! Must enter a number between 1 and 5
View 6 Replies
View Related
Jun 2, 2013
I need to run some operation if a key from keyboard is pressed. so I go with
Code: c=getchar();
to get it read. yet the user could press a key anytime; so I'd need some if-loop. no plans on how it'd look like though...I suppose something like this below wouldn't work right?
Code:
if(getchar()==1){
..
}
View 5 Replies
View Related
Jul 4, 2014
how can I allow pressing some keys on the keyboard in C++? I mean, when you are in the console and you are only allowed to press numbers before pressing enter, for example.
View 6 Replies
View Related
Mar 27, 2013
char name[10];To read a string from keyboard.
what should i use :
this : cin>>name[10];
or
this : cin>>*name;
View 3 Replies
View Related
Aug 31, 2013
I am trying to get my keyboard input into a function, so that I can call a function like updatebuttons() and set global variables to 0 if the button is not down at the moment and 1 if it is. It seems simple, but I can't seem to get it to work right; I am not use to working outside of windows, so I am still a beginner with SDL.
View 4 Replies
View Related