46 lines
818 B
C
46 lines
818 B
C
#include<stdio.h>
|
|
#include<fcntl.h>
|
|
#include<unistd.h>
|
|
#include<stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define szbuf 256
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
char buf[szbuf];
|
|
int a, b, c, d;
|
|
if(argc<2)
|
|
{
|
|
fprintf(stderr, "Usage: %s ./store_numb <file_name>",argv[1]);
|
|
exit(1);
|
|
}
|
|
a = open(argv[1],O_WRONLY|O_TRUNC|O_CREAT,0600);
|
|
if(a == -1)
|
|
{
|
|
perror("Opening destination file fails");
|
|
exit(2);
|
|
}
|
|
b = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600);
|
|
if(b == -1)
|
|
{
|
|
perror("Opening destination file fails");
|
|
exit(3);
|
|
}
|
|
write(1,"Numb --> ",9);
|
|
memset(buf,0,szbuf);
|
|
while(b = read(0,buf,szbuf))
|
|
{
|
|
d = (int)strtol(buf,NULL,0);
|
|
c = write(a,&d,sizeof(int));
|
|
if(c == -1)
|
|
{
|
|
perror("Writing in file fails");
|
|
exit(3);
|
|
}
|
|
memset(buf,0,szbuf);
|
|
write(1,"Numb --> ",9);
|
|
}
|
|
close(a);
|
|
exit(0);
|
|
} |