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

  • SEARCH
  • Home
  • 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 9207653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:26:06+00:00 2026-06-18T00:26:06+00:00

I’m writing a small sockets program (GNU libc). I have a loop which asks

  • 0

I’m writing a small sockets program (GNU libc). I have a loop which asks user for input (e.g. “MSG> “). When the user presses enter the message is sent (currently to a server on localhost).

Anyway, I want to read from stdin into a char buffer[256]. I’m currently using fgets() which doesn’t do what I want. I’m not sure how write the code such that I ask the user and then get data 256 -1 bytes at a time so that I can send a c-string of 1000 bytes via several strings of 256 bytes.

EDIT: Add code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 256

int main(int argc, char *argv[])
{
  char msg[BUFSIZE];
  size_t msgLen;

  msgLen = strlen(fgets(msg, BUFSIZE, stdin));
  puts(msg);

  // This simply checks whether we managed to fill the buffer and tries to get
  // more input
  while (msgLen == (BUFSIZE - 1))
    {
      memset (msg, '\0', BUFSIZE);
      fread(msg, BUFSIZE, 1, stdin);
      msg[BUFSIZE - 1] = '\0';
      msgLen = strlen(msg);
      puts(msg);
      if (msgLen < (BUFSIZE - 1))
    break;
    }

  return 0;
}
  • 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-06-18T00:26:07+00:00Added an answer on June 18, 2026 at 12:26 am

    You’re implementing a loop to ensure 1000 bytes are recieved, right? The loop should indicate that it’s counting up to 1000, for the sake of legibility. Keep track of the number of bytes you read (using the += operator), and use that number in the loop condition.

    You seem to assume that fread will read 255 bytes, but this is under the invalid assumption that 255 bytes are available. When less than 255 bytes are read, this doesn’t necessarily indicate an error; keep reading! When the return value of fread is less than zero, then you should be worried. Make sure you handle those situations.

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

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I need to clean up various Word 'smart' characters in user input, including but
I am trying to loop through a bunch of documents I have to put
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.