I have a quick little question involving the read() command.
I am really rusty in C, and unfortunately, my current assignment has me programming in C. We need to read stdin using the read() and not fgets and the like.
So I have a simple while loop:
int n, i;
char buffer[257], *input;
while((n=read(fileno(stdin), buffer, 256)) > 0)
{
buffer[n] ='\0';
if(buffer[n] = '\n') break;
write(input, buffer, strlen(buffer));
}
So I am not sure how to make this loop stop at the press of enter (end of line) so that's why I have the break code, though I don't know if that's done correctly.
The whole objective I am trying to accomplish is to put the input from stdin into the pointer 'input'
(I am really bad at understanding pointers so bear with me :) )
My problem is that I am getting segmentation faults when I press enter. I wish I could use fgets because then all this would be solved with a simple
input = fgets(buffer, 256, stdin);
Darn homework :( Anyways, if any of you could point me in the right direction, I would really appreciate it. Thank you!
if(buffer[n] = '\n') break;I think you intendedif(n && buffer[n-1] == '\n') break;( or maybe evenif(n==1 && buffer[n-1] == '\n') break;. Alsowrite(input, buffer, strlen(buffer));could be replaced bywrite(input, buffer, n);- wildplasserx = 1; if (x == 0) { /*...*/ }. Can you spot the problem? - Kerrek SB