READ function 2

Source: Internet
Author: User

READ function (Read File)

The READ function can read files. Reading a file refers to reading a certain number of characters from an opened object, and then placing these characters into a pre-stored buffer for future use.

The format is as follows:

Number = read (...

READ function (Read File)

The READ function can read files. Reading a file refers to reading a certain number of characters from an opened object, and then placing these characters into a pre-stored buffer for future use.

The format is as follows:

Number = read (handle, buffer, N );

In the preceding read call function, each parameter is defined as follows:

Handle: This is an opened file handle that reads data from the file represented by this file handle.

Buffer: refers to the buffer, that is, the read data will be placed in this buffer.

N: The number of characters that should be read when a read operation is called.

Number: the number of characters actually read by the system.

Assume that the length of a file is 600 characters, and the value of N is 512. When the file is read for 1st times, the system can normally read the content of the file with 512 characters, and pass the number of these characters to the number variable, so the value of number will change to 88. 2nd times to read this file, because the file has no content available for reading, the system will return 0 to number. In addition, if the file fails to be read, the system returns-1 to number.

For example, if a file contains 100 bytes and reads 10 bytes for the first time, the read pointer is at 10th bytes. When a 10-byte read operation is performed again, the first read position is followed by the next read. If you want to read data from the beginning, use the lseek function.

Code:

# Include "Lyl. H"

# Define Buf 512

Main ()

{

Static char filename [] = "t1.txt ";

Char buffer [Buf];

Int handle;

Int I;

Int Total = 0;

Handle = open (filename, o_rdonly );

If (handle =-1)

{

Printf ("[% s] Create fail !!!!

", Filename );

Exit (1 );

}

Else

{

While (I = read (handle, buffer, Buf)> 0)

Total = I;

}

Printf ("the total character in" % s "is % d

", Filename, total );

Exit (0 );

}

Program Execution result:

The total character in character t1.txt is 11

$ Cat t1.txt

1234567890

[Read System Call]

Function Description:
Read data from a file.

Usage:
# Include <unistd. h>

 

Ssize_t read (int fd, void * Buf, size_t count );


Parameters:
FD: Description of the file to be read.
Buf: the memory buffer of the read data.
Count: the amount of data to be read.

Return description:
When execution is successful, the data volume read is returned. -1 is returned for failure, and errno is set to one of the following values
Eagain: The o_nonblock flag is set when the file is opened, and no data can be read currently.
Ebadf: The file description is invalid or the file cannot be read.
Efault: The Buf points to a space that is inaccessible.
Eintr: The operation is interrupted by signals before data is read.
Einval: one or more parameters are invalid.
EIO: read/write error
Eisdir: directory of the FD Index

 

 

 

 

Note:

Read data from the serial port, read-only once, data is not fully read. After the while clause is added, all data is read.

Total_read_bytes = 0;

 

While (read_bytes = read (fd_485, buffer1, 256)> 0)

{

Memcpy (buffer2 + total_read_bytes, buffer1, read_bytes );

Total_read_bytes + = read_bytes;

}

 

For (I = 0; I <total_read_bytes; I ++)

Printf ("0x % 02lx/N", buffer2 [I]);

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.