Error calling system call through write

0

I try to make a system call with the word write but the compiler throws an error and says that error:

  

passing argument 1 of 'write' makes integer from pointer without a   cast

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
#include <conio.h>

int main(){
    uint64_t segundos;

    segundos = time(NULL);
    write(stdout, "Segundos desde 1970: %d",segundos);
    getch();
    return 0;
}
    
asked by Alejandro Caro 29.10.2017 в 18:47
source

1 answer

0

I think the function is misinterpreting the seconds, I mean, this is the syntax of write and the last parameter is the number of bytes to write

size_t write(int fildes, const void *buf, size_t nbytes);

You try to send the string as a second parameter but the function could be receiving that as the bytes, you can try to create the string before, besides the first parameter must be an int (0: standard input, 1: standard output, 2 : standard error).

I would do this to avoid the problem of the chain, I hope it serves you.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>

int main(){
    uint64_t segundos;

    segundos = time(NULL);
    char *cadena = (char*)malloc(33 * sizeof(char));
    sprintf(cadena, "Segundos desde 1970: %" PRIu64 "\n", segundos);
    write(1, cadena, sizeof(char) * 33);
    return 0;
}
    
answered by 29.10.2017 / 19:12
source