Wednesday, July 28, 2010

A TUTORIAL ON POINTERS AND ARRAYS IN C

A TUTORIAL ON POINTERS AND ARRAYS IN C

by Ted Jensen
Version 1.2 (HTML version)

Feb. 2000
This material is hereby placed in the public domain
Available in various formats via
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

參考來源:

"A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.2 (HTML version) Feb. 2000 This material is hereby placed in the public domain Available in various formats via http://pweb.netcom.com/~tjensen/ptr/cpoint.htm"
- A Tutorial on Pointers and Arrays in C (在「Google 網頁註解」中檢視)

Tuesday, July 27, 2010

1. 字符串操作函數

1. 字符串操作函數: "- Sent using Google Toolbar"

The C Programming Language

The C Programming Language



References

* The C Programming Language (second edition - ANSI C),
Brian W. Kernighan and Dennis M. Ritchie,
Prentice Hall, Inc., 1988.

* A Book on C (fourth edition),
Al Kelley and Ira Pohl,
Addison-Wesley, 1997.


Examples

Examples marked by the (advanced) symbol contain slightly more advanced material and will not be shown in class.

參考來源:

"Examples Examples marked by the symbol contain slightly more advanced material and will not be shown in class."
- The C Programming Language (在「Google 網頁註解」中檢視)

Friday, July 23, 2010

利用strstr和sscanf解析GPS信息

利用strstr和sscanf解析GPS信息

作者:杨硕,华清远见嵌入式学院讲师。

考察C程序员是否合格的一个重要标准就是看他操作字符串的能力,一个合格的C程序员应该可以熟练的对字符串进行拆分、组合、格式转换以及搜索定位,从一堆数据中提取出有效信息。

比如说我们要做一个GPS导航的项目,需要读取GPS模块以ASCII码的形式发送过来的数据,然后对这些数据进行处理,提取我们需要的信息。这就涉及到很多操作字符串的问题。下面就以此为例,利用strstr函数和sscanf函数解析GPS数据。

GPS输出的数据格式如下:
$GPGGA,121252.000,3937.3032,N,11611.6046,E,1,05,2.0,45.9,M,-5.7,M,,0000*77
$GPRMC,121252.000,A,3958.3032,N,11629.6046,E,15.15,359.95,070306,,,A*54
$GPVTG,359.95,T,,M,15.15,N,28.0,K,A*04
$GPGGA,121253.000,3937.3090,N,11611.6057,E,1,06,1.2,44.6,M,-5.7,M,,0000*72
$GPGSA,A,3,14,15,05,22,18,26,,,,,,,2.1,1.2,1.7*3D
$GPGSV,3,1,10,18,84,067,23,09,67,067,27,22,49,312,28,15,47,231,30*70
$GPGSV,3,2,10,21,32,199,23,14,25,272,24,05,21,140,32,26,14,070,20*7E
$GPGSV,3,3,10,29,07,074,,30,07,163,28*7D

可以看到,GPS模块发送过来的原始数据有很多,但是通常我们只需要其中的一部分信息就够用了,比如对于导航的功能,我们只需要以$GPRMC开头,以换行符结束的一行信息就够了。即:
$GPRMC,121252.000,A,3958.3032,N,11629.6046,E,15.15,359.95,070306,,,A*54

因此我们需要做的就是从读取的数据中截取以$GPRMC开头的一行信息,然后从中解析出经纬度、日期时间等有效信息即可。

假设从串口读取的数据存放在一个字符串指针char *raw_buf指向的内存单元里,首先我们通过ANSI C提供的strstr()函数找到以$GPRMC开头以换行符’\n’结束的字符串:
/* find "$GPRMC" from raw_buf */
if ((wellhandled_string = strstr(raw_buf, “$GPRMC”)) != NULL)
{
for (i=0; i

參考來源:

"利用strstr和sscanf解析GPS信息 作者:杨硕,华清远见嵌入式学院讲师。 考察C程序员是否合格的一个重要标准就是看他操作字符串的能力,一个合格的C程序员应该可以熟练的对字符串进行拆分、组合、格式转换以及搜索定位,从一堆数据中提取出有效信息。 比如说我们要做一个GPS导航的项目,需要读取GPS模块以ASCII码的形式发送过来的数据,然后对这些数据进行处理,提取我们需要的信息。这就涉及到很多操作字符串的问题。下面就以此为例,利用strstr函数和sscanf函数解析GPS数据。"
- 利用strstr和sscanf解析GPS信息-- 嵌入式学院(华清远见旗下品牌) (在「Google 網頁註解」中檢視)

Thursday, July 22, 2010

maven-javaeye

第一:举例说,你有个一依赖A用到了spring-2.0.5;后来spring在仓库换地方了(这种事情太多了,看看maven混乱的仓库吧),而你需要的是最新的spring2.5.1,那么你编译的时候就会同时有一堆spring2.0.5的文件和spring2.5的文件;那么你需要手动的排除 2.0.5的相关资源。问题是在maven体系里面,这种情况非常常见,大量的依赖混在一起的时候,你看着mvn install之后lib文件夹下乱七八糟的jar真是欲哭无泪啊,很多jar是同一个库的不同的版本,这些版本的依赖可能还不同,导致问题进一步的恶化。
第二:从eclipse的maven插件来说,add dependency的时候,一旦仓库更新了,你根本看不到最新的文件是是什么,比如struts2现在是2.0.11,但是你搜索的时候只能看到 2.0.9,因为我是那个时候装的;加入了这个dependency之后只能手动改版本到2.0.11再编译。
第三:开发者们同步仍然有困难。如果项目依赖一个仓库里面没有的文件你就完蛋了,手动拷贝到本地目录是个常见做法,如果你有100个人参与项目我想你要加入一个依赖简直大家都要崩溃了,比如struts2的json插件这个包。幸好还有artifactory,不过你一遍遍的手动发到私服上也够烦了。
maven的支持者们,我们是活在现实世界中的生命,maven的思想很好但是java本身缺乏版本管理机制并且maven也没有切实有效的解决这个问题,只能靠meta里面maven文件夹里面的描述信息,这个信息很多时候没有经过严格测试的(我遇到过几次了!导致项目根本没法编译过去,那个包的开发人员一定也用的是他自己的私服)!如果再这样下去我想很多人会崩溃的……
我仍然是maven的用户,但是我在瞪着眼睛等待一个新的工具,取代maven和ant。

參考來源:

"第一:举例说,你有个一依赖A用到了spring-2.0.5;后来spring在仓库换地方了(这种事情太多了,看看maven混乱的仓库吧),而你需要的是最新的spring2.5.1,那么你编译的时候就会同时有一堆spring2.0.5的文件和spring2.5的文件;那么你需要手动的排除2.0.5的相关资源。问题是在maven体系里面,这种情况非常常见,大量的依赖混在一起的时候,你看着mvn install之后lib文件夹下乱七八糟的jar真是欲哭无泪啊,很多jar是同一个库的不同的版本,这些版本的依赖可能还不同,导致问题进一步的恶化。"
- 关于Maven,不吐不快。 - develop - JavaEye论坛 (在「Google 網頁註解」中檢視)

qsort: sorting array of strings, integers and structs abstract

qsort: sorting array of strings, integers and structs
abstract
qsort() is standard C function for sorting arrays. It is defined by ISO C standard, and implemented in most C/C++ standard libraries(stdlib.h). This article contains an example of using qsort() for sorting integers, strings and structs.
compatible
# Any ANSI C compiler
# Should work with most C++ compilers

qsort() takes four arguments:

void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *));

* base — is a pointer to the beginning of data array
* nel — is a number of elements
* width — is a size of each element (in bytes)
* compar — is a callback function (pointer to function), which does comparison and returns positive or negative integer depending on result.

This example contains three separate functions sort_integers_example(), sort_cstrings_example() and sort_structs_example().

* sort_integers_example() uses int_cmp() as compar callback. Additional function print_int_array is used for printing integer array contents.
* sort_cstrings_example() uses cstring_cmp() as compar callback. Additional function print_cstring_array is used for printing string array contents.
* sort_structs_example() uses struct_cmp_by_price() and struct_cmp_by_product() as compar callbacks. Additional function print_struct_array is used for printing array of struct.

參考來源:

"qsort: sorting array of strings, integers and structs abstract  qsort() is standard C function for sorting arrays. It is defined by ISO C standard, and implemented in most C/C++ standard libraries(stdlib.h). This article contains an example of using qsort() for sorting integers, strings and structs."
- qsort: sorting array of strings, integers and structs (在「Google 網頁註解」中檢視)

結構(struct)的大小問題

結構(struct)的大小問題
在記憶體配置的時候,因為效率考量,預設會依照元素大小整數倍的對齊
並且會以其中最大的元素為單位,配置整數倍的記憶體
例如同樣是char, short, int三個元素

參考來源:

"結構(struct)的大小問題 在記憶體配置的時候,因為效率考量,預設會依照元素大小整數倍的對齊並且會以其中最大的元素為單位,配置整數倍的記憶體例如同樣是char, short, int三個元素"
- Coding James: 結構(struct)的大小問題 (在「Google 網頁註解」中檢視)

Structure 結構化的資料型態一

Structure
結構化的資料型態一

這一星期的課程,我們將面對C語言中比較深入的話題,怎麼表達複雜的資料型態,怎麼做到一言以蔽之,簡潔而有力的C語言寫法。還有,如果你覺得 int,char,float...不夠親切,你希望用更好的名稱來表達你的變數,這時候你需要typedef的語法。如果這都還不夠,還不能符合你的要求,還有以bit為單位的處理(bit manupulation),以及列舉(enumeration)的資料型態。

參考來源:

"Structure 結構化的資料型態一           這一星期的課程,我們將面對C語言中比較深入的話題,怎麼表達複雜的資料型態,怎麼做到一言以蔽之,簡潔而有力的C語言寫法。還有,如果你覺得int,char,float...不夠親切,你希望用更好的名稱來表達你的變數,這時候你需要typedef的語法。如果這都還不夠,還不能符合你的要求,還有以bit為單位的處理(bit manupulation),以及列舉(enumeration)的資料型態。"
- Structure I (在「Google 網頁註解」中檢視)

介紹如何將struct array寫入binary file,並從binary file讀出struct array

Abstract
本文介紹如何將struct array寫入binary file,並從binary file讀出struct array。

參考來源:

"Abstract 本文介紹如何將struct array寫入binary file,並從binary file讀出struct array。 Introduction C語言 / fwrite_fread_struct_array.c"
- (原創) 如何將struct array寫入文字檔? 如何從文字檔讀出struct array? (C/C++) (C) - 真 OO无双 之 真乱舞书 - 博客园 (在「Google 網頁註解」中檢視)

Wednesday, July 21, 2010

inception 電影評論

不錯的一句話
inception 電影評論

事實並不存在,只是我們,陷在其中。

參考來源:

"现实并不存在,只是我们,陷在其中——《Inception》 - [电影] 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://parasite.blogbus.com/logs/69692744.html"
- 现实并不存在,只是我们,陷在其中——《Inception》 - 我和E的宇宙飞船 - 博客大巴 (在「Google 網頁註解」中檢視)

What's the difference between text and binary I/O?

Q: What's the difference between text and binary I/O?

A: In text mode, a file is assumed to consist of lines of printable characters (perhaps including tabs). The routines in the stdio library (getc, putc, and all the rest) translate between the underlying system's end-of-line representation and the single \n used in C programs. C programs which simply read and write text therefore don't have to worry about the underlying system's newline conventions: when a C program writes a '\n', the stdio library writes the appropriate end-of-line indication, and when the stdio library detects an end-of-line while reading, it returns a single '\n' to the calling program. [footnote]

In binary mode, on the other hand, bytes are read and written between the program and the file without any interpretation. (On MS-DOS systems, binary mode also turns off testing for control-Z as an in-band end-of-file character.)

Text mode translations also affect the apparent size of a file as it's read. Because the characters read from and written to a file in text mode do not necessarily match the characters stored in the file exactly, the size of the file on disk may not always match the number of characters which can be read from it. Furthermore, for analogous reasons, the fseek and ftell functions do not necessarily deal in pure byte offsets from the beginning of the file. (Strictly speaking, in text mode, the offset values used by fseek and ftell should not be interpreted at all: a value returned by ftell should only be used as a later argument to fseek, and only values returned by ftell should be used as arguments to fseek.)

In binary mode, fseek and ftell do use pure byte offsets. However, some systems may have to append a number of null bytes at the end of a binary file to pad it out to a full record.

See also questions 12.37 and 19.12.

參考來源:

"Q: What's the difference between text and binary I/O?"
- Question 12.40 (在「Google 網頁註解」中檢視)

CSC 209 course notes -c 教學

CSC 209 course notes
Course notes available so far:

* Software tools
* Shell programming
* The unix filesystem
* The C programming language (intro)
* C data types; pointers and arrays
* C: printf and scanf
* argc and argv in C
* C strings
* C files
* getopt example
* struct, malloc
* what is being covered about unix processes
* Process memory layout and OS considerations
* Socket examples

(It's not being taught this term, but I have some detailed notes which I'm fond of about the AWK programming language, which is often used in shell scripts for various data processing and string munging tasks which are a bit more sophisticated than what cut and join and such can do.)

http://www.dgp.toronto.edu/~ajr/209/notes/

參考來源:

"CSC 209 course notes Course notes available so far:"
- CSC 209 course notes (在「Google 網頁註解」中檢視)

Tuesday, July 20, 2010

ptt scanf example

推 phstudy:%s改成%[^,] 07/17 19:16
推 dann1:成功了~ 大大可以解釋一下[^,]這個的意思嗎 07/17 19:35
推 dann1:研究了一下好像是規定字串結尾是逗號 那^的意思是無視嗎 07/17 19:39
推 TsinTa:^是not的意思,[^,]代表存取逗號外的所有字元 07/17 19:44
推 loveme00835:regular expression 07/17 19:48
→ loveme00835:可以搭配fread、fwrite, 把整個結構變數都用二進位方 07/17 20:13
→ loveme00835:式存放在檔案中, 這樣也可以支援隨機存取 07/17 20:14
→ loveme00835:http://ppt.cc/@qjm 07/17 20:15

參考來源:

"#include <stdio.h> #include <stdlib.h> typedef struct { int age; char name[ 10 ]; int weight; }Person; int main() { Person person = { 20, "Jane", 50 }; // 存資料進檔案 FILE *file = fopen( "test.txt", "wb" ); fwrite( &person, sizeof(Person), 1, file ); fclose( file );"
- Paste #281215 | LodgeIt! (在「Google 網頁註解」中檢視)

Monday, July 19, 2010

Q&amp;&amp;A

Q&&A

參考來源:

"Hi, I need a program in C that is capable of writing the multiple records into a file.And another function that should read the same data from the from the file and store it in the approptiate data structure."
- Read multiple records from a file and store it in a structure (在「Google 網頁註解」中檢視)

Program for sorting records in a file, Using linked list and insertion sort, records are sorted.

Program for sorting records in a file, Using linked list and insertion sort, records are sorted.

參考來源:

"Program for sorting records in a file, Using linked list and insertion sort, records are sorted."
- Program for sorting records in a file, Using linked list and insertion sort, records are sorted. « hackattacking.com (在「Google 網頁註解」中檢視)

rsort : Sort multi-line text records.

www.cs.duke.edu/~des/scripts/

#


# rsort : Sort multi-line text records.

The Scriptorium
Daniel E. Singer
Department of Computer Science
Duke University

參考來源:

"rsort : Sort multi-line text records."
- Dan's Scripts Page (在「Google 網頁註解」中檢視)

Mutiple-Line record in AWK

Mutiple-Line record in AWK
這是值得參考的名詞叫作多行的輸入。
other site:
http://www.math.utah.edu/docs/info/gawk_6.html#SEC34

參考來源:

"Multiple-Line Records In some databases, a single line cannot conveniently hold all the information in one entry."
- The GNU Awk User's Guide - Reading Input Files (在「Google 網頁註解」中檢視)

Creating Linked Lists in C++

Creating Linked Lists in C++
What is a linked list?
A linked list is a data structure which is built from structures and pointers. It forms a chain of "nodes" with pointers representing the links of the chain and holding the entire thing together. A linked list can be represented by a diagram like this one:
不錯的參考 C ++

參考來源:

"Creating Linked Lists in C++ What is a linked list? A linked list is a data structure which is built from structures and pointers. It forms a chain of "nodes" with pointers representing the links of the chain and holding the entire thing together. A linked list can be represented by a diagram like this one:"
- Creating Linked Lists in C++ (在「Google 網頁註解」中檢視)

Sorting Records in cprogramming board

Sorting Records
I am writing a program to sort records using a sorting algorithm of my choice. However before I can get to that I am having trouble reading in each record. Can someone provide some insight. I know that I want to do a linked list for easy insertion rather than an array, I just don't know where I am going wrong with my code. Thanks in advance!!

參考來源:

"I am writing a program to sort records using a sorting algorithm of my choice. However before I can get to that I am having trouble reading in each record. Can someone provide some insight. I know that I want to do a linked list for easy insertion rather than an array, I just don't know where I am going wrong with my code. Thanks in advance!!"
- Sorting Records (在「Google 網頁註解」中檢視)

sort record

這是另外一個sort record的 Q&A有程式碼

參考來源:

"Array Struct Sort Help!! Urgent! Expand Post » Ok i've tried understanding some previous posts but im just finding it really hard to work with structure passing to other functions and sorting. Can someone please explain in a simplistic on how i can pass structure to a function that sorts the structure by 'Year' or 'Artist' Below is the code i've done so far that allows a user to input records and those records are then put to a *.txt file....how can i proceed from there on..."
- Array Struct Sort Help!! Urgent! - C (在「Google 網頁註解」中檢視)