init commit
This commit is contained in:
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
char *fin = argv[1];
|
||||
char *fout = argv[2];
|
||||
|
||||
// printf("fdin: %d\n", fin);
|
||||
|
||||
int fdin = open(fin, O_RDONLY);
|
||||
if (fdin == -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fdout = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0777);
|
||||
for (;;) {
|
||||
char c[1024];
|
||||
int r = read(fdin, c, 1024);
|
||||
if (r == -1) {
|
||||
return 1;
|
||||
}
|
||||
if (r == 0) {
|
||||
break;
|
||||
}
|
||||
write(fdout, c, r);
|
||||
}
|
||||
|
||||
close(fdin);
|
||||
close(fdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+52
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fdin = open("in", O_RDWR);
|
||||
if (fdin == -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
double num1, num2;
|
||||
|
||||
// Read the first double
|
||||
if (read(fdin, &num1, sizeof(double)) != sizeof(double)) {
|
||||
close(fdin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read the second double
|
||||
if (read(fdin, &num2, sizeof(double)) != sizeof(double)) {
|
||||
close(fdin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Move the file pointer back to the beginning
|
||||
if (lseek(fdin, 0, SEEK_SET) == -1) {
|
||||
close(fdin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write the second double first
|
||||
if (write(fdin, &num2, sizeof(double)) != sizeof(double)) {
|
||||
close(fdin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write the first double second
|
||||
if (write(fdin, &num1, sizeof(double)) != sizeof(double)) {
|
||||
close(fdin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(fdin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fdout == -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned short x;
|
||||
|
||||
while (scanf("%hu", &x) == 1) {
|
||||
unsigned char buf[2];
|
||||
buf[0] = x >> 8;
|
||||
buf[1] = x & 0xff;
|
||||
write(fdout, &x, 2);
|
||||
}
|
||||
|
||||
close(fdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fdin = open(argv[1], O_RDONLY);
|
||||
if (fdin == -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned int res = 0, tmp;
|
||||
unsigned char num[4]; // не пишем в int, т.к. неизвестно, какой порядок байтов
|
||||
|
||||
while (read(fdin, num, 4) == 4) {
|
||||
// unsigned int tmp = (unsigned int) num << 24 | (unsigned int) num <<
|
||||
// 16 |
|
||||
// (unsigned int) num << 8 | (unsigned int) num;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
tmp = tmp << 8 | num[i];
|
||||
}
|
||||
res += tmp;
|
||||
}
|
||||
|
||||
printf("%u\n", res);
|
||||
|
||||
close(fdin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+1
@@ -0,0 +1 @@
|
||||
123 234
|
||||
Reference in New Issue
Block a user