Hello everyone.

I'm developing a program where I need to write some data on a FIFO file, and I'm using the command Write() for that.

My problem, is that with that command I can't send integers, so I was thinking about Convert them to Char, and then increment them on the array "buf[]" that I'm sending on the Write() command.

The string I want to send is suppose to be like this:
"17:00:00, 30.09.09, TE22.09, DR952.25, FE35.58, "
Where the values after TE, DR and FE are suppose to be the integer values, because they wont be fix values.

Don't know it it is the best solutions, or if anyone have a better suggestion.

Here is my code:

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <iostream>

#define BAUDRATE B9600
#define MODEMDEVICE "fifo"

  int fd, c, n, m;
  time_t     now;
  struct tm  ts;
  char       buf[51];

  if (fd <0) perror(MODEMDEVICE);

  m = 0;

  // Get the current time
  // Format and print the time, "HH:MM:SS, dd.mm.yy, "
  ts = *localtime(&now);
  strftime(buf, sizeof(buf), "%H:%M:%S, %d.%m.%y, ", &ts);

  //Sending data to FIFO
  n = write(fd, buf, sizeof(buf)-1);

  if (n < 0)
    puts("write() failed!");
  m = m+1;

  /* Make read() return immediately */
  fcntl(fd, F_SETFL, FNDELAY);


Best regards,

Re: How to Convert Int to Char (ANSI C) 80 80

Why can't you send integers with write....

int myint = 1234;

write(fd, (char*)&myint, sizeof(int));
Re: How to Convert Int to Char (ANSI C) 80 80

you can use sprintf() to format the string then write() to write that string to the file.

line 18: if the file can not be opened the program needs to do two things, not one.

if( id < 0)
   return 1; // abort the program
Re: How to Convert Int to Char (ANSI C) 80 80


I solved the problem with sprintf() and strcat() to concatenate all the data.

Thank you both and sorry for my noobish.

Best regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.