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"

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

  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  if (fd <0) perror(MODEMDEVICE);

  m = 0;
  while(m<10){

  // Get the current time
  time(&now);
  // 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;
  sleep(1);
  }

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

}

Best regards,
Daniel

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

int myint = 1234;

write(fd, (char*)&myint, sizeof(int));

Edited 6 Years Ago by gerard4143: n/a

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)
{
   perror(...);
   return 1; // abort the program
}

Hi.

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

Thank you both and sorry for my noobish.

Best regards,
Daniel

This question has already been answered. Start a new discussion instead.