Read From Buffer and Write to File
This is my first article in what I'm hoping volition be a serial of articles on system programming for POSIX compliant operating systems with focus on Linux. Actually I've touched this topic a while ago when I wrote three articles near library programming on Linux (static libraries, dynamic libraries and dynamic libraries using POSIX API). In this serial my goal is to go trough basics of Linux organization programming from the easiest topics like open file, read file and file write to a chip more complicated things like Berkeley sockets network programming. So lets become started with environment setup and an case of program that copies source file into destination file using POSIX API system calls to demonstrate open(), read() and write() system calls on Linux operating system.
Configuring your environs
I'll utilise my trustworthy Ubuntu Linux operating system but you tin can actually use any POSIX compliant operating organization, the only deviation volition probably be that you lot volition need to configure your environment differently. What we demand to begin with Linux arrangement programming is gcc compiler with related packages and POSIX related man pages. And then hither'south how to install this packages on Ubuntu based operating organization:
sudo apt-get install build-essential manpages manpages-dev manpages-posix manpages-posix-dev
Basically that's all you demand to create serious system tools for Linux operating system. After we will probably need some more libraries simply we will install them when necessary.
open up(), read() and write() system calls
Lets continue with our first system call open up()
whose purpose is to open file for reading or writing or to create new file. You should open it's man page if you haven't already washed so using man 2 open
control and read trough basics (2 is manual section number, use man human being
to read more nearly integrated manual section numbers). In the following example nosotros also employ read()
and write()
system calls to copy from 1 file descriptor to the other (both descriptors returned by open()
organization call) so it is wise to open their man pages also (human being 2 read
and homo two write
). And so hither'southward the example code for program that copies input file passed as first argument into output file passed every bit 2nd argument:
1 2 3 4 five six vii viii 9 ten xi 12 13 14 fifteen 16 17 18 xix xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /* ============================================================================ Proper name : sp_linux_copy.c Author : Marko Martinović Clarification : Copy input file into output file ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> #define BUF_SIZE 8192 int main( int argc, char * argv[ ] ) { int input_fd, output_fd; /* Input and output file descriptors */ ssize_t ret_in, ret_out; /* Number of bytes returned by read() and write() */ char buffer[BUF_SIZE] ; /* Character buffer */ /* Are src and dest file proper name arguments missing */ if (argc != three ) { printf ( "Usage: cp file1 file2" ) ; return ane ; } /* Create input file descriptor */ input_fd = open (argv [ 1 ] , O_RDONLY) ; if (input_fd == - 1 ) { perror ( "open" ) ; return 2 ; } /* Create output file descriptor */ output_fd = open up(argv[ 2 ] , O_WRONLY | O_CREAT, 0644 ) ; if (output_fd == - 1 ) { perror ( "open" ) ; return 3 ; } /* Copy process */ while ( (ret_in = read (input_fd, &buffer, BUF_SIZE) ) > 0 ) { ret_out = write (output_fd, &buffer, (ssize_t) ret_in) ; if (ret_out != ret_in) { /* Write error */ perror ( "write" ) ; return four ; } } /* Close file descriptors */ close (input_fd) ; close (output_fd) ; return (EXIT_SUCCESS) ; } |
If you have named this code file sp_linux_copy.c
and if you desire to name executable file sp_linux_copy
to compile this programme you would probably use something like this:
gcc -Wall -o sp_linux_copy sp_linux_copy.c
So if your source file is named source_file.txt
and if you lot want to proper noun the destination file destination_file.txt
you would run this program like this:
./sp_linux_copy source_file.txt destination_file.txt
At present lets go trough the code and explain catchy parts. Starting time thing we must do is to include necessary header files. Man page of every system telephone call tells y'all what header files you need to include to exist able to apply this system telephone call. Second nosotros volition ascertain constant we will apply to define size of our buffer in bytes. Smaller buffer size volition brand our copy process longer but it will save memory. Next we open up source and destination file descriptors, source with O_RDONLY
to make information technology read only, destination with O_WRONLY | O_CREAT
to make it writable and to create destination file with 0644 file organization permission flags. In case of mistake we apply perror() man 3 perror
to print relatively user friendly error bulletin.
Now we are ready to start copy process. We run read()
and write()
within loop (because source file might be bigger than our buffer) to copy from one file into another. Important to notice is that write()
is using number of bytes read from source file returned by read()
so it would know how much to write into destination file. If number of bytes read (ret_in) and number of bytes written (ret_out) differ this indicates error then once once again we use perror()
to print out mistake clarification. At the finish if all went well we do cleanup past closing both file descriptors and returning 0 (EXIT_SUCCESS) to betoken that plan ended without errors.
That'south it for this introductory article on Linux arrangement programming topic. In my next article I will show you lot few more examples on POSIX input/output so motility on to memory direction related arrangement calls.
Source: https://www.techytalk.info/linux-system-programming-open-file-read-file-and-write-file/
0 Response to "Read From Buffer and Write to File"
Post a Comment