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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:29:44+00:00 2026-05-13T14:29:44+00:00

I am trying to read in data from a text file (the time). and

  • 0

I am trying to read in data from a text file (the time). and convert that into something that can be DiffTime’ed to the current system time.

I am now so close to getting this working correctly, I can taste it but I am stuck with an issue I cannot work out. (I have a very basic grasp of the C language).

this program reads in the data from the text file, splits it into two char arrays, and then tries to use atoi to convert this into an integer. However, I am getting problems with the second atoi call.

From the bottom two printf statements, I should be getting:

12  
34

but for some reason i am getting something along these lines.

12  
3412

I presume that atoi is overunning the boundries of one of the char arrays or my char arrays are too long or too short. Either way, I cannot fathom what is going on.

#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  ihour;
    int  imin;

    int  STATE_OK = 0;
    int  STATE_WARNING  = 1;
    int  STATE_CRITICAL = 2;
    int  STATE_UNKNOWN  = 3;
    /* indicates if system is paused 1 = System is paused, 0 = System running */
    int  system_paused  = 0; 

    char filebuf[5]; 
    char hourbuf[2];
    char minbufer[2];

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

    printf("filebuffer %s\n", filebuf);

    hourbuf[0] = filebuf[0];
    hourbuf[1] = filebuf[1];

    printf("Hour Buffer %c%c\n", hourbuf[0],hourbuf[1]);

    minbufer[0] = filebuf[2];
    minbufer[1] = filebuf[3];

    printf("Min Buffer %c%c\n", minbufer[0],minbufer[1]);

    imin = atoi(minbufer);
    ihour = atoi(hourbuf);


    printf("hour as int %d\n", ihour);
    printf("min as int %d\n", imin);

    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-05-13T14:29:44+00:00Added an answer on May 13, 2026 at 2:29 pm

    You forgot to terminate your char buffers with zero.

    char hourbuf[2];
    char minbufer[2];
    

    Should be

    char hourbuf[3];
    char minbufer[3];
    
    hourbuf[2] = '\0';
    minbufer[2] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to read data from a text file and loading it into a
I'm trying to read data from a binary file and put it into a
I'm trying to read data from a text file and assign it to arrays.
I am trying to read in data from a text file and do a
I am trying to read in data from a text file using numpy.loadtxt with
I'm trying to read data from a text file, clear it, and then write
I am trying to read Data from a Text file & storing it inside
I am trying to read data from Excel file into my windows application. Connection
I'm trying to read data from an xml file and display it in a
I'm trying to read data from excel using POI. How can I check if

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.