1

I have data in my .txt file containing different time ins and time outs of employees. For example, 10:20 but I initially designed the structure to have their data types to be of char arrays or string. Since I'll be using the time values in another function, I have to use the atoi() function to convert them into integer values. Problem is, there is a colon : in each of the time values. Would it be possible to convert the string 10:20 to an integer using atoi() so that I can it in my future functions? Does the use of atoi() allow some splitting or some sort so that I can convert my time value from string to int?

I tried

char time[10] = "10:20";

int val;

printf("string val = %s, int value = %d", time, atoi(time));

But my output is only string val = 10:20, int value = 10 so only the string before the : is read and converted to string. I would want that after converting, I would stil have 10:20 as the result but in integer because I am going to use relational operators with it.

Niic
  • 175
  • 1
  • 1
  • 11
  • 1
    "I have to use the atio() function to convert them into integer values". No, you don't. In fact, you should never use `atoi`, since it produces undefined behavior on certain inputs. Use `strtol`. – William Pursell Jun 09 '21 at 15:30
  • @Nic: What would you want the contents of `val` to be when `time == "10:20"`? – Bill Lynch Jun 09 '21 at 15:31
  • What integer do you want to use to represent the string "10:20"? Do you want to convert it to minutes past midnight, or seconds past midnight, or an epoch time incorporating the date, or something else? – William Pursell Jun 09 '21 at 15:32
  • @WilliamPursell Problem still persists regardless if I use strtol. My 10:20 string only becomes 10 after conversion. – Niic Jun 09 '21 at 15:33
  • `atoi(time)` == 10, `atoi(strchr(time, ':') + 1)` == 20 (I think, haven't tested), so you could maybe use 2 `atoi` calls. – mediocrevegetable1 Jun 09 '21 at 15:34
  • @BillLynch Still 10:20, but in integers as I would need to use their values with relational operators – Niic Jun 09 '21 at 15:34
  • 3
    `strtol` will tell you the address of the character after `10`. Check if this is `':'`, go to the next character and from there use a second call to `strtol` to convert `20`. Or if the string can be modified, use `strtok` to split it into tokens delimited by `:`. – Bodo Jun 09 '21 at 15:36
  • What integer should `10:20` become? `1020`? – Aykhan Hagverdili Jun 09 '21 at 15:38
  • `10:20` cannot be an integer as written. What are you trying to get out of the conversion? – Nordii Jun 09 '21 at 15:38
  • @AyxanHaqverdili Yes. Both William and mediocrevegetable's solutions work. – Niic Jun 09 '21 at 15:39
  • [Why shouldn't I use atoi()? (duplicate)](https://stackoverflow.com/q/17710018/995714) – phuclv Jun 09 '21 at 16:08

2 Answers2

1

It's not clear what you actually want, but maybe something like:

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

int
main(int argc, char **argv)
{
        char *time = argc > 1 ? argv[1] : "10:20";
        int d;
        char *e;

        d = strtol(time, &e, 10);
        if( *e == ':' ){
                d *= 100;
                d += strtol(e + 1, &e, 10);
        }
        if( *e != '\0' ){
                fprintf(stderr, "invalid input\n");
                return 1;
        }

        printf("string val = %s, int value = %d\n", time, d);
        return 0;
}

This will produce d = 1020 for the string "10:20". It's not at all clear to me what integer you want to produce, but that seems to be what you're looking for.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

You can also use sscanf:

#include <stdio.h>

int main() {
  char const* time = "10:20";

  int h, m;
  if (sscanf(time, "%d:%d", &h, &m) != 2)
    return 1;

  printf("string val = %s, int value = %d\n", time, h * 100 + m);
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93