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 7583165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:36:59+00:00 2026-05-30T18:36:59+00:00

i am using a Unix machine and i am trying to read from console

  • 0

i am using a Unix machine and i am trying to read from console till EOF is reached (am supplying with Ctrl+D) . I am using fread_unlocked, For the input read, it outputs the read integers correclty but instead of a normal exit , it gives segmentation fault on EOF . How do i modify my code so that it behaves as expected?

int MAXX = 10000000;
char *ipos, InpFile[MAXX];

inline int input_int(int flag=0)
{
  while(*ipos<=32)
    ++ipos;
 if(flag)
    return(*ipos++-'0');
 LL x=0,neg=0;
 char c;
 while(true)
  {
    c=*ipos++;
    if(c=='-')
        neg=1;
    else
    {
        if(c<=32)
         return neg?-x:x;x=(x<<1)+(x<<3)+c-'0';
    }
  }
 }
int main()
{
   ipos = InpFile;
   fread_unlocked(InpFile, MAXX, 1, stdin);
   while(true){
   int n = input_int();
   printf("%d\n",n);
   }
   return 0;
}   

My input from the console is : 3 4 5 6Ctrl+D
THe output i get now is : 3 4 5 6 Segmentation Error
Expected output: 3 4 5 6
Thanks.

  • 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-30T18:37:00+00:00Added an answer on May 30, 2026 at 6:37 pm

    fread_unlocked returns the number of bytes that were actually read. You need to take that return value, and you need to make sure that you never try to use more than that many characters out of InpFile. For example, if you declare max_ipos at global scope, you might write:

    size_t bytes_read = fread_unlocked(InpFile, 1, MAXX, stdin);
    // check for errors
    max_ipos = &InpFile[bytes_read];
    

    and then input_int will need to detect when ipos == max_ipos and terminate before reading *ipos.

    Edited to add: Note that (at the suggestion of Jonathan Leffler) I’ve switched the order of the arguments 1 and MAXX to fread_unlocked. This is because you want to read objects of size 1, not objects of size MAXX.

    Incidentally, this:

    inline int input_int(int flag=0)
    

    is not valid C. Default values for arguments are a C++ thing. (Maybe there are C compilers that support it as an extension — I don’t know — but there are definitely C compilers that don’t.)

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

Sidebar

Related Questions

I am using Eclipse 3.5 on Unix machine. I am trying to execute a
I am trying this code to download a file from a Windows machine using
I'm developing a huge console application for Unix using C# via Mono. If I
I have created a repository on Unix server and trying to connect it using
am trying to run a Java file on Unix machine. Let me show the
I'm trying to put in a Tomcat server on our unix machine (access remotely)
Using UNIX socket APIs on Linux, is there any way to guarantee that I
I have a site which I want to download using Unix wget . If
Using the unix dig command, I can get the following MX record: $ dig
I've been using the unix timestamp all my life. I like it because it's

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.