user3052078
user3052078

Reputation: 495

Linux write integer

I want to read from file a number and increase the number than write it to file. I want to use read/write not fscanf/fprintf .I tried to change integer to string but i found on Google that the itoa doesn't exist in linux . This is my code :

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(int argc,char *argv[]){
    int f;
    f=open(argv[1],O_RDWR);
    char c[10];
    read(f,c,10);
    int ceva=atoi(c);
    printf("%d ",ceva);
    ceva++;
    //itoa (ceva,c,10);
    lseek(f,0,SEEK_SET);
    write(f,ceva,sizeof(int));
}

Upvotes: 0

Views: 819

Answers (1)

user3553031
user3553031

Reputation: 6214

What do you have against fprintf? You need to convert the integer to a string somehow. Another option would be to use snprintf to convert the integer to a string then write the string.

Upvotes: 3

Related Questions