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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:19:56+00:00 2026-06-04T13:19:56+00:00

I have a program that accept orders by reading commands from a file. In

  • 0

I have a program that accept orders by reading commands from a file.
In this file some commands are “float string”, like “1.0”,”2.0″, but they are invalid, what the program need is integer, like “1”,”2″. So, how can I make the program understand the commands like “1.0” is invalid? Is there any neat way to do this?

    char buf[CMDSIZE];
    if(fgets(buf, CMDSIZE, stdin))      //buf likes this: "1.0 \n"
    {
        *prio = 1; *command = -1; *ratio =1.0;
        // I need to make sure that command is not "1.0" or something like this              
        sscanf(buf, "%d", command);
        switch(*command){....blahblah......}
    }

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-06-04T13:19:58+00:00Added an answer on June 4, 2026 at 1:19 pm

    It’s easier to use strtol.

    This will parse a base-10 integer. The pointer e will point to the first character after the integer. You can check to make sure it’s a NUL byte and signal an error otherwise. (You also have to check that the input isn’t empty.)

    If you want to allow spaces / newlines after the number, you can do that too. Note that strtol eats leading whitespace — but not trailing whitespace.

    long v;
    char *e;
    v = strtol(buf, &e, 10);
    if (!buf[0] || *e) {
        error();
    }
    // v has number
    

    Footnote: Checking for overflow and underflow with strtol is a little weird. You have to set errno to 0 first, call strtol, then check if the result is LONG_MIN or LONG_MAX and if errno is set to ERANGE.

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

Sidebar

Related Questions

I have a program that looks something like this: public partial class It {
Lets say I have a server program that can accept connections from 10 (or
I have a program that downloads PCM data from a Web Server, which it
I have a program that has this code : #include<stdio.h> main(){ int input; char
I have an ipod touch program that should receive messages from a server program
I have a program that accepts two file names as arguments: it reads the
I currently have a program that basically reads html from a webpage. After sending
Say I want a have a C program that does this: 1. User will
I have written a simple program that use some classes and procol buffers. These
I have a program that can accept command-line arguments and I want to access

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.