Archive

Archive for September, 2011

那些曾有的感悟(一)

September 25th, 2011 3 comments

2011-09-25:翻看一个多月前游玩时拍的照片,顿时觉得人生真如梦一场,不停地转场,那些人来了又走了,那些景近了又远了。不变的是你还是你,依旧在梦醒时尝试去回忆,却已记不得什么,发现回不去了,留下的只是藏于心底深处的一丝亲切。待到相逢时,也许会再次变得清晰,此情此景,我曾有过。

2011-08-14:季节交替的时候最容易伤感。凌晨回来时,屯里的气息让我想起了08年刚到美国的情景,来美已经三周年多了,因为出游而忘了写篇周年记。

Read more…

Categories: 五味杂陈 Tags: ,

步步惊心

September 24th, 2011 6 comments

最近步步惊心很火,火得我也看了。很多人觉得奇怪,我一个大男生竟然也看。好片子,大家都可以看的,管他男的女的。不过,步步帅哥比较多,确实比较适合女生看。算一下,看过的穿越剧有三部了,古天乐演的《寻秦记》,胡歌演的《神话》,和《步步惊心》。现在不追美剧了,没有什么想看的。TBBT很久没看了,Vampire Diaries第三季出了,但也没去看。追剧集比较累,还是等整季都出了再看。步步惊心之前还看了一部陈小春演的《碧波仙子》,大失所望,但还是把它给看完了。会看这个也是因为陈小春,想着他演的《鹿鼎记》,以为这部会是好片,不想却是烂片。

APUE2e Exercise3.2

September 11th, 2011 2 comments

My implementation for APUE2e Exercise3.2: implement my own dup2 function that performs the same service as the dup2 function. Based on a little testing, it seems to work and behave as the provided dup2 function. I used the dup function to implement mydup2 function. However, I didn’t work on the error handling part, just succeed or fail.
Read more…

Categories: 书山有路 Tags: , ,

APUE2e Exercise3.6

September 11th, 2011 No comments

I was working exercise3.6 of APUE2e and the code was simple. But when I tried to use fgets and fputs to print out the file content, I got problems. Then I tried to figure out the reason. It turned out to be the null or ‘’ byte of C string, which prevent fgets and fputs from behaving as I expected (fgets appends null byte to the end of input buffer, while fputs prints null-terminated string). I solved the problem by preventing write function to write the null or ‘‘ byte for a C string to the file. This was done by restricting the number of bytes to be writen (using sizeof(buffer)-1, rather than sizeof(buffer)). These findings reminded me of my careless reading, because the problem was caused by a really small mistake. Now I learned the lesson. I prefer reading to writing codes. But through writing and testing, I learn more than just reading. I think I must do more coding and don’t get satisfied just by reading the books and codes.

Attaching is the code I wrote for exercise3.6.
Read more…

我给你自由

September 6th, 2011 2 comments

Categories: 我的最爱 Tags: ,

A test on mkstemp

September 2nd, 2011 No comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * testmkstemp.c
 *
 *  Created on: Aug 31, 2011
 *      Author: zhuhuang
 */
 
//int mkstemp(char *template);
 
#include <apueerr.h>
int main(void)
{
	int filedes;
	char *temp1 = "/home/zhuhuang/try1XXXXXX";
	char temp2[] = "/home/zhuhuang/try2XXXXXX";
 
	if((filedes = mkstemp(temp2)) == -1) //Succeed.
		err_sys("mkstemp error");
	printf("temp2: %sn", temp2);
 
	if((filedes = mkstemp(temp1)) == -1) //Fail!!!!!
		err_sys("mkstemp error");
	printf("temp1: %sn", temp1);
 
	exit(0);
}

The output is shown below:

temp2: /home/zhuhuang/try2anKX9s

Pay attention to the second call of mkstemp on temp1. It failed. Check the definition of temp1 and temp2, we will know the reason. For temp1, it is a pointer to a string literal “/home/zhuhuang/try1XXXXXX”, which is placed somewhere in the memory. The string literal is a constant. We can not use pointers that point to it to change its contents. That’s why mkstemp failed, since it tried to modify the contents pointed by temp1 to the name it has generated. For temp2, it is an array of characters. The string literal “/home/zhuhuang/try2XXXXXX” is used to initialized array temp2. But the contents of the array can be changed. So first call of mkstemp on temp2 succeeded.

Here is a page that will be useful for understanding character pointers: http://c-faq.com/~scs/cclass/krnotes/sx8e.html

Categories: 书山有路 Tags: , , ,

Something about setbuf

September 1st, 2011 2 comments

When I worked on Exercise 5.1 of APUE.2e, I made some discoveries about function setbuf : void setbuf(FILE *restrict fp, char *restrict buf).

First, setbuf doesn’t check whether buf has a size of BUFSIZ. It leaves the work to users to make sure that buf has a size of BUFSIZ. I thought it would check!!!

Second, when the stream to be set is associated with a terminal device, such as stdin, stdout and stderr, setbuf doesn’t set them to line buffered (when buf is not NULL), but fully buffered. My system is Ubuntu 10.04 (2.6.32-33-generic).

Below is the program I worked out. A litter messy.
Read more…

Categories: 书山有路 Tags: , , , ,