1.File reader and write
2.命令列引數
1. File reader and write
// Read the file :
FILE *fr
fr=fopen("a.txt","r");
// Read the text line
char s[80];
fgets(s,80,fr);
Method 2: // we can use the pointer to replace the file name
char fileName[80]="a.txt"
fr=fopen(fileName,"r");
File Write:
FILE *fw = fopen("b.txt","w");
//write the file to fw
fclose(fw); //每次寫完記得要關閉檔案指標,才會將buffer的內容寫到b.txt中.
ref:http://www.csie.ntu.edu.tw/~cprog2003/downloads/Notes%20on%20C%20File%20I-O.htm
2.命令列引數
在c中 ,命令列引數最多可以有5個 (including the program name) , 這個數目包含了程式名稱 , for example :
app hello.c -o -c project.p
(其中app為程式名稱)
則argc會記錄為5 , 代表有5個argument.
其中 argv[0] = "app" argv[1]="hello.c" , argv[2]="-o" , argv[3] ="-c" , argv[4]="project.p"
ref :http://caterpillar.onlyfun.net/Gossip/CppGossip/CommandArg.html
3. 傳入二維陣列
ref:http://www.cnblogs.com/oomusou/archive/2008/03/24/1119362.html
4.qsort:
呼叫
qsort( (void*) array , Num , sizeof( int ) , compareInt );
定義
int compareInt(const void *a, const void *b)
{
int c = *(int *)a;
int d = *(int *)b;
if(c < d) //傳回 -1 代表 a < b
return -1;
else if (c == d) //傳回 0 代表 a = b
return 0;
return 1; //傳回 1 代表 a>b
}
使用qsort進行排序
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *s1, const void *s2);
int main(void)
{
char test[] = "qwertyuioplkjhgfdsazxcvbnm";
qsort(test, 26, sizeof(char), cmp);
printf("%s\n", test);
return 0;
}
int cmp(const void *s1, const void *s2)
{
return *(char *)s1 - *(char *)s2;
}
5. 浮點數精確問題
6. 外部宣告與內部宣告對記憶體的差異
7.以binary方式進行檔案讀取
8.%f and %g
9.除以0,造成segmentation fault
10.用int array與unsigned char寫入file的差異
11.將數字轉成string的方法 , 使用sprintf
留言列表