I'm currently trying to write an integer variable using write function in the file. This is my code:
int main(int argc, char** argv){
int fd, nbMult, i;
char buf[4];
if((fd = open("data", O_CREAT|O_RDWR, 0777))==-1){
perror("ERROR\n");
exit(EXIT_FAILURE);
}
do{
printf("Bla bla ");
if( scanf("%d", &nbMult) !=1 ) while( (i = getchar()) != '\n' );
}while(nbMult<1);
sprintf(buf, "%d", nbMult);
if( write(fd, buf, sizeof(int)) == -1 ){
perror("ERROR\n");
exit(EXIT_FAILURE);
}
close(fd);
return 0; }
It creates the file, but when I try to open it, it shows me "cannot display ". When I remplace the name of the file by data.txt it works but it shows strange character in the file. I have tried also to convert the Integer variable to String using sprintf() and it's the same issue.