Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:26:19+00:00 2026-05-31T04:26:19+00:00

I have a quick little question involving the read() command. I am really rusty

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:26:20+00:00Added an answer on May 31, 2026 at 4:26 am

    You’re going about it all wrong. Firstly, write is a system call that’s used to write to a open file descriptor. Your input is nothing like that – a file descriptor is gained by calling the open syscall first, or referring to one of the always-open files, e.g. stdin, stdout or stderr. Not to mention that a file descriptor is an int on Linux.

    Also, you should remember that the assumption of your input on stdin ending with a newline doesn’t have to be right all the time. Your program may receive some content on standard input that doesn’t contain any newlines, for example contents of some file (redirected to stdin), or the input from keyboard may simply end with a Ctrl-D (EOF) instead of the “return” key.

    On top of all that, an uninitialized pointer doesn’t refer to any valid memory. The declaration of char *input doesn’t give you any right to write/read the memory referred to by input unless you allocate some memory that this pointer will point to first.

    Copying strings in C is achieved by using functions declared in <string.h>. They operate on C-strings, e.g. sequences of characters terminated by \0. In this case, read() doesn’t terminate its output by a \0, in which case you’ll want to use the memcpy function instead. Why don’t you simply try reading directly into input, though? It doesn’t make much sense to read to buffer first, simply to copy the data to another chunk of memory later on.

    Also, the specification of read states that it reads up to count bytes from the file descriptor. Which means that if your buffer is declared as char[257], then you should call read(stdin,buffer,257), not 256. It would probably also be more suitable to simply declare the buffer as a 256-element array.

    I hope that I’ve straightened out some stuff for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick little question... I know that sometimes in other languages libraries have part of
Quick question: I've run into a little issue, where I just need some insight
I have quick question for you SQL gurus. I have existing tables without primary
I have a quick question about encapsulating specific types with typedef . Say I
I have a quick question regarding debug and release in VS 2008. I have
I have a quick question about the dealloc() and viewDidUnload() methods. I notice a
I have a quick question about the accelerometer in Android devices. Is it always
I have a quick question here. I know that the cakePHP find('first') function returns
I have a quick question. I'm have a db an audit Table with a
I have a quick question about the layout of the page on a custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.