In Linux, call the syscils. h header file

Source: Internet
Author: User

Finally, I started to read chapter 8 of the C, the UNIX system interface! This is an exciting chapter. After learning it, you can start to write UNIX programs, such as printing directories, viewing file sizes, and file attributes, that is to say, you can write some practical small programs. Some system functions, such as read () and write (), are the basis of Unix programming. In addition, I also explained an implementation method of malloc. I think this will deepen my understanding of memory.

However, the first example made me dumb.

# Include "syscils. H"

Main ()
{
Char Buf [bufsiz];
Int N;

While (n = read (0, Buf, bufsiz)> 0)
Write (1, Buf, N );
Return 0;
}

Compilation Error

K @ Bian :~ $ Gcc-wall test. c
Test. C: 1: 22: syscils. h: no such file or directory
Test. C: 5: Error: 'bufsiz' undeclared (first use in this function)
Test. C: 8: Warning: Implicit declaration of function 'read'
Test. C: 9: Warning: Implicit declaration of function 'write'

Read (), write (), and bufsiz are defined in syscils. h.

I open/usr/include/and check that there is no syscils. H! However, there is a syscall. H. If this parameter is changed, an error is returned.

Linux is not UNIX after all, and I was a little scared that this chapter could not be learned. In this case, Google is required!

You can use man to check the solution.

Man read

Haha! Heran says

# Include <unistd. h>

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

But there are still problems

K @ Bian :~ $ Gcc-wall test. c
Test. C: 5: Error: 'bufsiz' undeclared (first use in this function)

At this moment, I suddenly came up with a way to use grep!

K @ Bian :~ $ Grep bufsiz/usr/include /*
/Usr/include/_ g_config.h: # DEFINE _ g_bufsiz 8192
/Usr/include/libio. h: # DEFINE _ io_bufsiz _ g_bufsiz
/Usr/include/stdio. h: # ifndef bufsiz
/Usr/include/stdio. h: # define bufsiz _ io_bufsiz
/Usr/include/stdio. h: else make it use buffer Buf, of size bufsiz .*/

Originally in stdio. H!

The program is changed to this mode to solve the problem.

# Include <stdio. h>
# Include <unistd. h>

Main ()
{
Char Buf [bufsiz];
Int N;

While (n = read (0, Buf, bufsiz)> 0)
Write (1, Buf, N );
Return 0;
}

Good! You can continue learning: d

Find this, the gnu c library manual, which is certainly useful for programming in Linux. Download is available in several formats. We recommend that you download formatted in HTML (976 K gzipped tar file) with one web page per node.

 

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.