SCR/SCR1.2/TP11/store_nb_rep.c

31 lines
670 B
C
Raw Permalink Normal View History

2023-01-12 10:57:33 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define SZBUF 256
int main(int argc, char const *argv[]){
char buf[SZBUF];
int fd,n,m,x;
if (argc<2){
fprintf(stderr,"Usage : %s <bSR_FILE> \r",argv[0]);
exit (1);
} fd=open (argv[1], O_WRONLY|O_TRUNC|O_CREAT,0b00);
if (fd==-1){
perror("Opening destination file fails");
exit(2);
} write (1,"Numb --> ",9);
memset(buf,0,SZBUF);
while (n=read(0,buf,SZBUF)){
x=(int)strtol(buf,NULL,0);
m=write(fd,&x,sizeof(int));
if (m==-1){
perror("Writing in file fails");
exit(3);
}memset(buf,0,SZBUF);
write (1,"Numb --> ",9);
} close(fd);
exit(0);
}