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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:49:38+00:00 2026-05-13T11:49:38+00:00

need some advice on this one as im struggling abit and cannot figure it

  • 0

need some advice on this one as im struggling abit and cannot figure it out.

i have a file that gets updated on a PC to indicate a system ran and what time it ran. i am writing a very simple linux console app (will eventually be a nagios plugin). that reads this file and responds depending on what it found within the file.

i am a total newbie to programming on Linux and using C so please be patient and if you would explain any answers it would really be appreciated.

basically i want to convert a char array containing 5 characters into an integer, however the 5th char in the array is always a letter. so technically all i want to-do is convert the first 4 chars in the array to a integer… how?? ive tried multiple ways with no success, my problem is that presently i do not have a good grasp of the language so have no real ideas on what it can and cannot do.

here is the source to my program.

basically the buf array will be holding a string taken from the file that will look something like this

3455Y (the number will be random but always 4 chars long).

Sorry for the poor formatting of the code, but i cannot get this stupid window for love nor money to format it correctly….

include <fcntl.h>
include <unistd.h>
include <stdio.h>
include <stdlib.h>
include <time.h>
include <string.h>

define COPYMODE 0644

int main(int argc, char *argv[])  
{
  int  i, nRead, fd;
  int  source;
  int  STATE_OK = 0;
  int  STATE_WARNING  = 1;
  int  STATE_CRITICAL = 2;
  int  STATE_UNKNOWN  = 3;
  int  system_paused  = 0; 

  char buf[5]; 
  int  testnumber;

  if((fd = open(argv[1], O_RDONLY)) == -1)
    {
      printf("failed open : %s", argv[1]);
      return STATE_UNKNOWN;
    }
      else
    {
      nRead = read(fd, buf, 5);
    }

  close(source);

  if (buf[4] == 'P')
    {
      printf("Software Paused");
      return STATE_WARNING;
    }
      else
    {
      return STATE_OK;
    }
    time_t ltime; /* calendar time */  
    struct tm *Tm;
    ltime=time(NULL); /* get current cal time */  
    Tm=localtime(&ltime);


    int test;
    test = Tm->tm_hour + Tm->tm_min;
    printf("%d", test);

    printf("%d", strtoi(buf));

}
  • 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-13T11:49:39+00:00Added an answer on May 13, 2026 at 11:49 am

    You can use atoi

    atoi requires one char * argument and returns an int.
    If the string is empty, or first character isn’t a number or a minus sign, then atoi returns 0.If atoi encounters a non-number character, it returns the number formed up until that point

    int num = atoi(buf);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some expect advice on how to handle the following:- I have a
I need some help from the shell-script gurus out there. I have a .txt
I need some advice how to go about this: 1) I retrieve an XML
I've been thinking about this one for quite some time now, I need a
Need some advice on creating a floating menu that behaves very similar to what
HI there, this is a little sticky situation and I need some advice. I
I need some advice in building a Java server that handles multiple clients at
I need some math advice... Or, at least I think i do. I have
I need some advice as to how I easily can separate test runs for
I need some advice to save me re-inventing the wheel. Our windows app is

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.