read system-call

  • Thread starter =?ISO-8859-1?Q?Andreas_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

Hi,

I'm working on a Linux-programm with a signal-handler and therfore I
need to use the read-system-call instead of cin.get or sth. like that.
With getline my program works fine, it reads a line from stdin and
passes it to a buffer but when I try to replace getline with read, sth.
is wrong!? can u help me?

//this works fine
length=cin.getline(cmd_buffer1,512).gcount();
for (z=0;z<length;++z){
cmd_buffer[z]=cmd_buffer1[z];
cmd_buffer[length]='\0';

//using read instead if cin.getline !?
length=read(STDIN_FILENO, cmd_buffer1, 80);


the reason why I use the for-loop (which I won't need when using
getline) is that I think I have to set the termination char (\0) myself
(when using read), is that correct?
 
J

John Harrison

Andreas said:
Hi,

I'm working on a Linux-programm with a signal-handler and therfore I
need to use the read-system-call instead of cin.get or sth. like that.
With getline my program works fine, it reads a line from stdin and
passes it to a buffer but when I try to replace getline with read, sth.
is wrong!? can u help me?

//this works fine
length=cin.getline(cmd_buffer1,512).gcount();
for (z=0;z<length;++z){
cmd_buffer[z]=cmd_buffer1[z];
cmd_buffer[length]='\0';

//using read instead if cin.getline !?
length=read(STDIN_FILENO, cmd_buffer1, 80);


the reason why I use the for-loop (which I won't need when using
getline) is that I think I have to set the termination char (\0) myself
(when using read), is that correct?

Getline stops reading when it gets to the end of a line, read just reads
how many bytes you tell it to read. So in your example read will always
read 80 bytes, no matter where the end of the line is.

john
 
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

John said:
Andreas said:
Hi,

I'm working on a Linux-programm with a signal-handler and therfore I
need to use the read-system-call instead of cin.get or sth. like that.
With getline my program works fine, it reads a line from stdin and
passes it to a buffer but when I try to replace getline with read, sth.
is wrong!? can u help me?

//this works fine
length=cin.getline(cmd_buffer1,512).gcount();
for (z=0;z<length;++z){
cmd_buffer[z]=cmd_buffer1[z];
cmd_buffer[length]='\0';

//using read instead if cin.getline !?
length=read(STDIN_FILENO, cmd_buffer1, 80);


the reason why I use the for-loop (which I won't need when using
getline) is that I think I have to set the termination char (\0) myself
(when using read), is that correct?


Getline stops reading when it gets to the end of a line, read just reads
how many bytes you tell it to read. So in your example read will always
read 80 bytes, no matter where the end of the line is.

ya, this is why I copy all the chars in a separate array.
 
M

Maxim Yegorushkin

Andreas said:
Hi,

I'm working on a Linux-programm with a signal-handler and therfore I
need to use the read-system-call instead of cin.get or sth. like that.
With getline my program works fine, it reads a line from stdin and
passes it to a buffer but when I try to replace getline with read, sth.
is wrong!? can u help me?

//this works fine
length=cin.getline(cmd_buffer1,512).gcount();
for (z=0;z<length;++z){
cmd_buffer[z]=cmd_buffer1[z];
cmd_buffer[length]='\0';

//using read instead if cin.getline !?
length=read(STDIN_FILENO, cmd_buffer1, 80);

You can easily wrap a file descriptor with iostream interface, just
like I did it for FILE*. http://rsdn.ru/Forum/?mid=942482
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
IRMNikole

Latest Threads

Top