Linux C files and directories 3 files read and write

Source: Internet
Author: User

File read/write

File read and write refers to the reading of information from a file or writing information to a file. Linux file reads can be implemented using the Read function, and file writes can be implemented using the Write function. when a file is written, it is only manipulated in the buffer of the file and may not be written to the file immediately. You need to use the sync or Fsync function to write the buffer's data to a file .

File Write operations:

function write can write a string to an already opened file, this function is used as follows:

ssize_t Write (int fd, void *buf, size_t count);

Parameters:

FD: The file number of the file that has been opened.

BUF: the string to write.

Count: An integer that requires the number of characters to be written. Represents the number of bytes that need to be written to the content.

return value:

[[email protected] exercise]$ cat write.c
#include <stdio.h>
#include <unistd.h>
# Include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h
#include <errno.h>
int main (void)
{
int fd;
Char path[] = "Txt1.txt";
Char s[]= "Hello". .";
extern int errno;
FD = open (path, o_wronly| O_creat| O_trunc, 0766);
if (fd! =-1)
{
printf ("opened file%s. \ n", path);
}
Else
{
printf ("Can ' t Open file%s.\n", path);                          
printf ("errno:%d\n", errno),                 &N Bsp        //print error number
printf ("ERR:%s\n", Strerror (errno));             Print the information corresponding to the error number.
}
Write (fd, S, sizeof (s)),
Close (FD);
printf ("done\n");
return 0;
}

[Email protected] exercise]$./write
Opened file Txt1.txt.
Done

Reading the file function read

The function read reads a string from an open file.

ssize_t Read (int fd, void *buf, size_t count);

Parameter: FD: Represents the number of the file that has been opened.

BUF: is a null pointer, and the read content is returned to the string that the pointer points to.

Count: Indicates the number of characters that need to be read.

Return value: Returns the number of characters read to. A return value of 0 indicates that the end of the file has been reached or that no content is readable in the file.

FD = open (path, o_rdonly);
if (FD! =-1)
{
printf ("opened file%s. \ n", path);
}
Else
{
printf ("Can ' t Open file%s.\n", path);
printf ("errno:%d\n", errno);
printf ("ERR:%s\n", Strerror (errno));
}
if (size = read (fd, str, sizeof (STR))) < 0)
{
printf ("ERR:%s", strerror (size)); If there is a mistake, print the error message by error number.
}
Else
{
printf ("%s\n", str);
printf ("%d\n", size);
}
Close (FD);
return 0;
}

Result

  

Opened file Txt1.txt.
Hello ...
10

Linux C files and directories 3 files read and write

Related Article

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.