39 lines
808 B
C
39 lines
808 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#define STDOUT 1
|
|
#define STDERR 2
|
|
#define SZBUF 256
|
|
|
|
|
|
int main(int argc, char** argv){
|
|
int descripteur, x;
|
|
ssize_t echec;
|
|
char buf[SZBUF];
|
|
if (argc < 2){
|
|
fprintf(stderr, "Usage : %s <file-name>\n",argv[0]);
|
|
exit(1);
|
|
}
|
|
descripteur = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0600);
|
|
if (descripteur==-1){
|
|
perror("opening file fail !");
|
|
exit(2);
|
|
}
|
|
write(1,"Numb --> ",9);
|
|
memset(buf,0,SZBUF);
|
|
while (echec = read(0,buf,SZBUF)){
|
|
x = (int) strtol(buf,NULL,0);
|
|
echec = write(descripteur,&x,4);
|
|
if (echec==-1){
|
|
perror("writing in file failed!");
|
|
exit(3);
|
|
}
|
|
write(1,"Numb --> ",9);
|
|
memset(buf,0,SZBUF);
|
|
}
|
|
close(descripteur);
|
|
exit(0);
|
|
}
|