Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/File-input-output/C/file-input-output-1.c
Normal file
27
Task/File-input-output/C/file-input-output-1.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *in, *out;
|
||||
int c;
|
||||
|
||||
in = fopen("input.txt", "r");
|
||||
if (!in) {
|
||||
fprintf(stderr, "Error opening input.txt for reading.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
out = fopen("output.txt", "w");
|
||||
if (!out) {
|
||||
fprintf(stderr, "Error opening output.txt for writing.\n");
|
||||
fclose(in);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while ((c = fgetc(in)) != EOF) {
|
||||
fputc(c, out);
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
fclose(in);
|
||||
return 0;
|
||||
}
|
||||
36
Task/File-input-output/C/file-input-output-2.c
Normal file
36
Task/File-input-output/C/file-input-output-2.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* we just return a yes/no status; caller can check errno */
|
||||
int copy_file(const char *in, const char *out)
|
||||
{
|
||||
int ret = 0;
|
||||
int fin, fout;
|
||||
ssize_t len;
|
||||
char *buf[4096]; /* buffer size, some multiple of block size preferred */
|
||||
struct stat st;
|
||||
|
||||
if ((fin = open(in, O_RDONLY)) == -1) return 0;
|
||||
if (fstat(fin, &st)) goto bail;
|
||||
|
||||
/* open output with same permission */
|
||||
fout = open(out, O_WRONLY|O_CREAT|O_TRUNC, st.st_mode & 0777);
|
||||
if (fout == -1) goto bail;
|
||||
|
||||
while ((len = read(fin, buf, 4096)) > 0)
|
||||
write(fout, buf, len);
|
||||
|
||||
ret = len ? 0 : 1; /* last read should be 0 */
|
||||
|
||||
bail: if (fin != -1) close(fin);
|
||||
if (fout != -1) close(fout);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
copy_file("infile", "outfile");
|
||||
return 0;
|
||||
}
|
||||
23
Task/File-input-output/C/file-input-output-3.c
Normal file
23
Task/File-input-output/C/file-input-output-3.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
int copy_file(const char *in, const char *out)
|
||||
{
|
||||
int ret = 0;
|
||||
int fin, fout;
|
||||
char *bi;
|
||||
struct stat st;
|
||||
|
||||
if ((fin = open(in, O_RDONLY)) == -1) return 0;
|
||||
if (fstat(fin, &st)) goto bail;
|
||||
|
||||
fout = open(out, O_WRONLY|O_CREAT|O_TRUNC, st.st_mode & 0777);
|
||||
if (fout == -1) goto bail;
|
||||
|
||||
bi = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fin, 0);
|
||||
|
||||
ret = (bi == (void*)-1)
|
||||
? 0 : (write(fout, bi, st.st_size) == st.st_size);
|
||||
|
||||
bail: if (fin != -1) close(fin);
|
||||
if (fout != -1) close(fout);
|
||||
if (bi != (void*)-1) munmap(bi, st.st_size);
|
||||
return ret;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue