The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Question about NULL Character & fgets() f.ben.isaac High Level Programming 2 11-04-2008 12:31 PM
Problem with handling SIGINT JamesGoh High Level Programming 3 02-24-2008 10:39 PM
Problem with fgets and rewind function .. user_prady High Level Programming 1 02-08-2008 05:20 AM
Cannot catch SIGINT while serial break condition occurs gzz High Level Programming 13 11-23-2007 08:06 AM
fgets() skanky High Level Programming 4 09-11-2002 10:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-01-2009
hurricane86 hurricane86 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
[C] fgets problem with SIGINT singlal!!!

Hi all,

I have this method to read a string from a STDIN:

PHP Code:
void readLine(charinputBuffer){

    
fgets (inputBufferMAX_LINEstdin);
    
fflush(stdin);

    
/* remove '\n' char from string */
    
if(strlen(inputBuffer) != 0)
        
inputBuffer[strlen(inputBuffer)-1] = '\0';


All work fine but if i catch SIGINT signal (CTRL+C) with this method:

PHP Code:
void handle_SIGNINT(){

    
/* here i don't want exit with program!!! */


i would like that "fgets's wait" was released when there is a signal....now to realese it i need press ENTER KEY!!!, how can i do in automate?

can i "simulate" the ENTER key pressed writing a particular ASCII char into STDIN???

Thanks a lot in advance, Martin
  #2 (permalink)  
Old 01-02-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,763
Code:
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void readLine(char *inputBuffer)
{
    char *p=NULL;
    *inputbuffer=0x0;
    if(fgets (inputBuffer, MAX_LINE, stdin)==NULL)
    {
        if(feof(stdin)) return;
    	if(errno == EINTR)
    	{
    	    *readLine=0x0; /* or do what you want to a partial read */
    		return;
    	}
    	/* other errors */
    	perror("Fatal error on stdin");
    	exit(1);    	
    }
    /* fflush(stdin) is NOT a defined operation 
       if this actually works it is "programming by accident" 
       
    */		
    fflush(stdin);

    /* remove '\n' char from string */
    /* if the string entered is greater than MAX_LINE
       then you are whacking off valid characters
       
    if(strlen(inputBuffer) != 0)
        inputBuffer[strlen(inputBuffer)-1] = '\0';
        I am assuming MAX_LINE is defined by you and is not some system value
        try this instead:
    */
    p=strchr(inputBuffer, '\n');
    if(p!=NULL)
    	*p=0x0;

}
fgets handles SIGINT all by itself - don't need to call a signal handler for SIGINT or leave the SIGINT handler as it is by adding code as above.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0