How does Linux C programming use online help to solve programming problems?

Source: Internet
Author: User

1. Background

many times to learn C language has been unable to step into the C language gate, each time in the C language of those system call library functions and so on, but only, Linux system calls need us to remember some irregular structure and some uppercase macro definition and there is a function of various forms such as print fprintf sprintf and so on, looks very uncomfortable, writing things to really very silent. Write what you want, write what you want to do to make the error-handling judgment. (Some of the C-language rookie complaints do not spray), and recently picked up C to learn the C programming under Linux. At present, mainly look at the Linux programming (early buy book), Unix/linux Programming Practice (Electronic version) of the UNIX Environment Advanced Programming (electronic version), ready to start unix/linux programming practice mainly look at this book the other two auxiliary view. When I was looking at the Unix/linux programming practice, I first flipped through the catalogue. It makes me feel fresh, not like the kind of books, the book is supplemented by practical examples to explain, each case involves a lot of system calls. Through the example does not tightly learn to use the system call also mastered the programming skills. Recently I just saw chapter two, in the second chapter, the book explains how to solve programming problems through Linux online help, so I really have a high interest in this book. You don't have to learn a lot of boring system calls and library functions from the start to write some small programs.


2. How to Achieve

For example, how to achieve the WHO command to write, for me such a rookie really do not know how to do.

1. So the first step is to see how the WHO command itself is implemented.

He found such a sentence in the man's description.

If FILE is not specified, use/var/run/utmp. /var/log/wtmp as FILE is common

The general meaning is that the who is to read a file to display information, if you do not specify this file, the default is/var/run/utmp and/var/log/wtmp two files.

2. Then immediately search for utmp wtmp related man information

Man-k utmp

Man-k wtmp

The results of the search are all pointing to the #include<utmp.h>

3. Then take a look at what this header file contains

           struct utmp {                short   ut_type;               /* type of record  */               pid_t    ut_pid;               / * pid of login process */                char    ut_line[ut_linesize]; /* device name  of tty -  "/dev/"  */                char    ut_id[4];              /* terminal name suffix,                                                   or inittab (5)  ID */                char    ut_user[ut_namesize]; /* username  */               char     ut_host[UT_HOSTSIZE]; /* Hostname for remote login, or                                                   kernel version for run-level                                                   messages */                struct  exit_status ut_exit;  /* exit  status of a process                                                   marked as DEAD_PROCESS; not                                                  used by  Linux init (8)  */                /* The ut_session and ut_tv fields must be the same  size when                   compiled 32- and 64-bit.  this allows data files and  shared                   memory to be shared between 32- and 64-bit applications.  */            #if  __wordsize == 64  && defined __wordsize_compat32                int32_t ut_session;           /*  session id  (GetSID (2)),                                                   used for windowing */                struct {                    int32_t tv_sec;            /* Seconds */                    int32_t tv_usec;          /*  microseconds */                } ut_tv;                       /* Time entry was made */             #else                  long   ut_session;            /* Session ID */                 struct timeval ut_tv;         /* Time entry was made */             #enDif               int32_t ut_ Addr_v6[4];        /* internet address of remote                                                   host; ipv4 address uses                                                   just ut_addr_v6[0] */                char __unused[20];             /* reserved for future use */            };

A UTMP structure comes out, roughly guessed that the data stored in/var/log/utmp or/var/log/wtmp is the data of this structure. So the train of thought comes out, read this file each time on a struct size data. Then output the desired member variable in the result body is OK. So what's left is the problem of program writing.

3. Summary of Practice

Just see the second chapter finally still a little confidence to continue to learn, not like two years ago learning C language as confused, know how to solve the problem under Linux how to seek help. Hope that through the form of blog to record their own C language learning some of the sentiment bar.

This article is from the "Focus on Linux" blog, so be sure to keep this source http://forlinux.blog.51cto.com/8001278/1435127

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.