Read/write files for file I/O operations-based on Linux system file IO

Source: Internet
Author: User

1.

Read files that are already open

#include <unistd.h>ssize_t read (int fd,void *buf,size_t count);

Returns the number of bytes read, if it has returned 0 at the bottom of the file, an error returned-1

BUF is a pointer to a buffer that holds the data that will be read to the terminal, and count indicates the number of bytes to be read

2.

Write data to an open file

#include <unistd.h>ssize_t write (int fd,void *buf,size_t count);

Returns the number of bytes already written, with an error of 1

If you specify the O_append option, the end of the file is positioned before each write

Example:

1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5#include <unistd.h>6#include <string.h>7#include <stdlib.h>8 9 #defineFILENAME "Hello"Ten #defineBuf_size 80 One #defineFLAGS o_rdwr| O_append A intMainvoid) - { -     intcount; the     intFD; -     CharWrite_buff[buf_size]; -     Const Char*pathname=FILENAME; -     if((Fd=open (pathname,flags)) ==-1) +     { -printf"Error"); +Exit1); A     } atprintf"Ok,has opened file\n"); -printf"start to write:\n"); -  -scanf"%[^\n]", Write_buff); -  -Count=strlen (write_buff); in     if(Write (Fd,write_buff,count) ==-1) -     { toprintf"error to write\n"); +Exit0); -     } theprintf"OK have write to file\n"); *     return 0; $}

Read/write files for file I/O operations-based on Linux system file IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.