Saturday, September 25, 2010

C mixed programming with scripting



Write programs in linux, do network of people, more or less of several scripts.鑴氭湰璇█鐏垫椿鐨勫彉閲忕被鍨嬨?寮哄ぇ鐨勬鍒欒〃杈惧紡澶勭悊鑳藉姏锛屽啀鍔犱笂linux绯荤粺鏈韩鐨勭閬撱?閲嶅畾鍚戜互鍙婁赴瀵岀殑鍛戒护琛屽伐鍏凤紝璁╀綘缂栫▼璧锋潵娓稿垉鏈変綑銆?br />
The C language does have many advantages, but it is undeniable that many occasions, more convenient to use scripting language, such as we will illustrate the handling of the configuration file.

Take a look at our sample program tasks:

Suppose we have a program written with c, it has a configuration file user.conf, save some user information, user.conf defined as follows:

1) to comment line # at the beginning of the act, not treated

2) to allow blank lines

3), if not 1 and 2, it is valid data in the following form

# User.conf: configure file for user
銆??# username age backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp country
tom 20 male us
銆??chen 22 female cn
Each column is divided into four fields, between fields with one or more blank characters (spaces or tabs) separate the field followed by the name, age, gender, national

Our c procedures to be completed on user.conf the add, delete, edit, query

Such a simple task, together with c processing is not complicated, but also to spend something about, and if the scripting language used to do, very simply, can call the script in the c done it?

銆??Awk鏄痩inux涓婁竴绉嶈剼鏈瑷?紝瀹冪殑闀垮鍦ㄤ簬澶勭悊鏈変竴瀹氭牸寮忚鍒欑殑鏂囦欢锛屼緥濡傚挶浠殑user.conf銆?Information on awk, many, oreilly company out of a special awk programming books, the Internet also can be downloaded. You can also look directly man awk.

Let's see how combine with the shell awk to accomplish these tasks:

1) Add a record

銆??渚嬪锛岃娣诲姞 jack 18 male us 杩欐牱涓?潯璁板綍锛屽彲浠ョ畝鍗曠殑鐢ㄩ噸瀹氬悜鍔熻兘

Echo Ce "jack 18 male us">> user.conf
銆??鐜板湪锛岃繖鏉¤褰曡娣诲姞鍒?user.conf鏈熬浜嗐?

2) Delete a record

For example, the information you want to delete the user chen

cat user.conf awk '! / ^ chen: blank: + / print'> tmp.conf; mvCftmp.conf user.conf
3), editing a record

銆??鐜板湪锛屾兂鎶?tom鐨勬?鍒敼涓?female

Through the system () this function, we can call the above script in c to complete the task.

However, system () still feel uncomfortable with them, the lack of it is only for scripting, but can not get the script output data, which is usually our source of data for further processing. (In the shell and perl, you can by anti-quotation marks (``) to get the command output). One solution is to redirect output to a temporary file, then read the file in the c, to obtain data, and finally of course, delete this file. However, this method always make people feel a little uncomfortable, if the output directly to the script execution in the data input to our buffer to the better.

I wrote a small function, called my_system (), through pipes and redirection, to achieve the above ideas.鍑芥暟鍘熷瀷濡備笅锛?br />
銆??int my_system(const char* pCmd, char* pResult, int size)锛?br />Output data is saved to the buffer pointed to by pResult, the buffer size for the size, can save up to size-1 data.

Functions to achieve the final article on

With this function after calling the script in the c more convenient, we can use it to achieve user.conf inquiries.

4), query a record

銆??渚嬪锛屾垜浠鑾峰彇 tom 鐨勬?鍒?br />
銆??鍙互鐢ㄨ剼鏈繖鏍锋潵瀹炵幇锛?br />
銆??cat user.conf awk 鈥?^tom:blank:+/ print $3鈥?br />銆??鑴氭湰鐨勬墽琛岀粨鏋滄槸 tom鐨勬?鍒?male琚緭鍑哄埌灞忓箷涓?br />
銆??鍦ㄦ垜浠殑 c绋嬪簭涓紝濡傛璋冪敤 my_system()锛?br />
char buf101;
銆??my_system(鈥渃at user.conf awk 鈥?^tom:blank:+/ print $3鈥欌?,buf,101);
After the call finished, buf the data is the "male" a, how, pretty easy right?

銆??浠ヤ笂鍙槸鐢ㄧ粨鍚堣剼鏈畬鎴愪簡涓?釜姣旇緝绠?崟鐨勪换鍔★紝鎵?互鎴戞病鏈夋妸杩欎簺鑴氭湰鍗曠嫭褰㈡垚鑴氭湰鏂囦欢銆?If you are good at using perl, shell, awk, you can write a more powerful script file to handle more complex problems, then a similar my_system () method, in c / c + + and other languages to obtain the script output to achieve interesting "hybrid programming."

I hope you can have fun!

# Include
# Include
# Include
# Include
# Include
銆??static int my_system(const char* pCmd, char* pResult,intsize)
銆??int fd2;
int pid;
int count;
int center;
char * p = 0;
int maxlen = size C 1;
memset (pResult, 0, size);
printf (pipe error \ n);
return C1;
/ / Chile process
int fd22;
printf (pipe2 error \ n);
return C1;
close (1);
dup2 (fd21, 1);
close (fd0);
close (fd21);
銆??system(pCmd);
read (fd20, pResult, maxlen);
pResultstrlen (pResult) -1 = 0;
write (fd1, pResult, strlen (pResult));
close (fd20);
exit (0);
/ / Parent process
close (fd1);
p = pResult;
center = maxlen;
銆??while((count = read(fd0, p, center)))
p + = count;
center -= count;
break;
close (fd0);
return 0;
int main (void)
char result1025;
my_system (/ sbin / ifconfig, result, 1025);
printf (the result is \ n \ n% s \ n, result);
return 0;







相关链接:



DVR-MS to MPEG



MKV to Xbox 360



Review 0



AMD Intel on EU antitrust penalties comment



The secret of growth



Kodak V705 Dual Lens Camera Stabilization Cheap Listing Weekend



Family can offer car parking bus transfer brush 2 yuan per CARD



Internal control: harm than good, or at every step



WiMAX and TD-band battle began deemed to be an advantageous position in China



QuickTime To MPEG



Training Of New Employees Approach HR



Master Is Not Highly Educated Does Not Mean That High-income Elite



Servlet's Context and Config



Terminal And Telnet Clients evaluation



Convert for



Thursday, September 16, 2010

Job 10 kinds of unhealthy attitude towards students


Shyness. Lost in the written submissions and run job site, facing recruiters stammer, blush, such a person is naturally uncomfortable employer appreciated.

Career psychology. "Learning and the Official Career," an official is the only proper that, Qiaojiannaodai to "Yamen" drill, did not realize that these places are the relationship between strength and Competition, well very people who can enter, and the results are mostly ending up first broken blood flow.

Mental comparisons. Some students say "level", during which I feel results in the campus than you, honor more than you, "official" than you, of course, work should be better than you. But I do not know the employer does not judge talent as the sole criterion, the keen rivalry of the "outstanding student" can only in the "standing above the crowd" to appreciate the lonely days and lonely.

Psychotherapy. Some students lack of independence, go out to see the Federation Ella parents, classmates and companions, or a buddy to help co-applicants the same unit by fixing the future take care of each other, this non-assertive and enterprising graduates will be the employer abandoned.

Psychological dependence. They do not hurry to find work, which climbed all day thinking about the relationship between relatives and friends, get some money to buy a job, so I am afraid that difficult to do long posts bought.

Local mental.杩欎簺澶у鐢熶笉鎰垮嚭杩滈棬锛屽彧鎰垮湪鐪煎墠鐨勨?涓?憨涓夊垎鍦扳?閲屽氨涓氾紝鍙︿竴浜涗汉鍒欐棭鏃╃櫥涓婄埍鎯呮柟鑸燂紝姣曚笟鍚庝负涓庡彟涓?崐鐣欏畧鍚屼竴鎴樺鑰屾瀹堜竴鏂癸紝杩欐牱鐨勪汉榧犵洰瀵稿厜锛岄毦鏈変綔涓恒?

Conservative psychology. Lack of sense of competition, not the challenge, or holding modesty "virtue" without letting go, afraid to reveal their own strengths and characteristics of the tiger air, loyalty are not, such people are not naturally favored employers.

Low on the psychological.涓庝繚瀹堝績鐞嗙浉鍙嶏紝杩欎簺浜烘?瑙夊緱绔炰簤婵?儓锛岃嚜宸辨妧涓嶅浜猴紝閬傜敇鎷滀笅椋庯紝涓嶆暍瀵硅嚜宸扁?鏄庣爜鏍囦环鈥濓紝鎵句釜涔板鑽夎崏鍗栧嚭銆?For some units out of the unequal agreements entered into with eyes closed, serious risks to future work.
World-weary psychological. These students thought radical, trendy, always put "cool", did not go out looking for work, low wages and there too badly treated here, one that is their job to the boss, do not say you can not find the money owed was also down a bunch of money.

False psychology. Fake degree, fake certificates, fake honor, etc. are not knocking on the door of employment straw, can not fake after all long, but only harm their reputation, destroy their future.






相关链接:



Font Tools Report



Ts format



Using VMware, Vowed To Computer "cloned A Technique"



Flv to dat converter



Wizard Newsgroup Clients



Master IS not highly educated does not mean that high-income elite



Business IS business had to kind



youtube m4v



Organization Process definition



Ad Blockers evaluation



Landscape engineering company in Yantai in hand Weaver



Convert f4v to flv



Photo 6 Of A Tough, For The First Magic



IM: Decisive FORCE'S crowded



"Tomato Garden" case of first instance pronounced loss of Chengdu, a total of over 10 million SOFT



Paladin examination and Topics for 4 years (1)



My Favorite Site Administration