Article View: es.comp.lenguajes.c++
Article #45Re: Leer caracteres
From: Pascal J. Bourguignon
Date: Mon, 13 Oct 2008 09:40
Date: Mon, 13 Oct 2008 09:40
87 lines
3823 bytes
3823 bytes
num11 <quintos_11@hotmail.com> writes:
> Saludos a todos, tengo un problema:
> quiero leer una secuencia (arreglo ) de caracteres hasta que no se de
> un "F", es decir, algo as�:
> i=0;
> do{
> scanf("%c",&c[i]);
> j=i;
> i++;
> }while(c[j]!='F')
�Qu� quieres? �Leer hasta que se de un 'F', o hasta que no se de un 'F'?
Tu codigo hace lo contrario de lo que escribes ariba...
> pero el problema es que hasta que no de un "enter" no me lee el
> caracter en "X" posici�n, lo que quiero es que me vaya leyendo los
> caracteres sin tener que dar el "enter", y que cuando escriba la "F"
> autom�ticamente (sin dar enter) finalice el programa. Alguna idea?? De
> antemano, gracias!.
Eso depende del sistema en el que tu programa funciona.
En lugar de usar una funci�n como scanf que trabaja con las
entrada/salidas con muelle, tendras que llamar una funci�n del sistema
para recibir los caracterios directamente.
En general, hay dos niveles de muelle, uno en tu programa, en la
biblioteca stdio (o stream si usarias las E/S C++), y otro en el
sistema, al nivel del piloto del terminal o teclado. Hace falta
traversar los dos.
En un sistema POSIX.1-2001, se puede usar las funciones siguiente:
tcgetattr(), tcsetattr(), tcsendbreak(), tcdrain(), tcflush(),
tcflow(), cfgetispeed(), cfgetospeed(), cfsetispeed(), and
cfsetospeed()
Por ejemplo, el siguiente programa POSIX.1-2001 leer la entrada
estandard hasta que no se de un 'F':
extern "C"{
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
}
void setRaw(int fd){
struct termios raw;
int res;
cfmakeraw(&raw);
res=tcsetattr(fd,TCSANOW,&raw);
if(res!=0){
perror("tcsetattr");
exit(1);
}
}
unsigned char readChar(int fd){
for(;;){
unsigned char buffer;
int res=read(fd,&buffer,sizeof(buffer));
switch(res){
case 0: break; // try again;
case 1: return(buffer);
default:
perror("read");
exit(1);
}
}
}
const int inpfd=0;
const int outfd=1;
const int errfd=2;
int main(void){
unsigned char c;
setRaw(inpfd);
while('F'==(c=readChar(inpfd)));
printf("Got a non F: '%c'\n",c);
return(0);
}
--
__Pascal Bourguignon__
Technical Details
Message-ID:
<7c4p3g96j9.fsf@pbourguignon.anevia.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!proxad.net!feeder1-2.proxad.net!cleanfeed1-a.proxad.net!nnrp6-1.free.fr!not-for-mailReferences:
<e30b42cf-4483-46ce-910a-86fdfcb5d51f@b1g2000hsg.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f53ec,41024634405f2e4f
X-Google-Attributes: gidf53ec,domainid0,public,usenet
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!proxad.net!feeder1-2.proxad.net!cleanfeed1-a.proxad.net!nnrp6-1.free.fr!not-for-mail
Newsgroups: es.comp.lenguajes.c++
Subject: Re: Leer caracteres
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA
oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9
033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac
l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR
mV+hO/VvFAAAAABJRU5ErkJggg==
X-Accept-Language: fr, es, en
X-Disabled: X-No-Archive: no
References: <e30b42cf-4483-46ce-910a-86fdfcb5d51f@b1g2000hsg.googlegroups.com>
From: pjb@informatimago.com (Pascal J. Bourguignon)
Date: Mon, 13 Oct 2008 11:40:58 +0200
Message-ID: <7c4p3g96j9.fsf@pbourguignon.anevia.com>
Organization: Anevia SAS
User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.2 (gnu/linux)
Cancel-Lock: sha1:MTgyY2Q2ZDY0MjgwMjFlYTU3MzQxNTg5YzY2YTAzMTk4MWNhMGFmNg==
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Lines: 87
NNTP-Posting-Date: 13 Oct 2008 11:40:59 MEST
NNTP-Posting-Host: 88.170.236.224
X-Trace: 1223890859 news-2.free.fr 22239 88.170.236.224:43804
X-Complaints-To: abuse@proxad.net