1. errno.h的頭文件簡介
errno宏定義為一個int型態的左值, 包含任何函式使用errno功能所產生的上一個錯誤碼。
一些表示錯誤碼,定義為整數值的宏:
EDOM 源自於函式的參數超出范圍,例如sqrt(-1)
ERANGE 源自於函式的結果超出范圍,例如strtol(0xfffffffff,NULL,0)
EILSEQ 源自於不合法的字元順序,例如wcstombs(str, L ff, 2)
查看錯誤代碼errno是調試程序的一個重要方法。當linux C api函數發生異常時,一般會將errno變數(需include errno.h)賦一個整數值,不同的值表示不同的含義,可以通過查看該值推測出錯的原因。在實際編程中用這一招解決了不少原本看來莫名其妙的問題。比較 麻煩的是每次都要去linux源代碼裡面查找錯誤代碼的含義,現在把它貼出來,以後需要查時就來這里看了。
2. 如何用C讀取文本文檔中的某一行
沒有能讀一行的吧,如果能用C語言實現你的要求,加分的話我給你做。先說明我不會讀一行的,因為一行有大小,比如你打開一個文本,當你縮小的時候行數會增多,放大文本的時候行數就減小,這樣你怎麼能保證每次讀出的數據都一樣呢?
以下是我對本題的答案:
/*************************
註:要求文件名必須和程序里寫的一樣,否則打開會失敗
輸入格式如:name sex age 中間的空格已處理(隨便幾個)
可直接在文本文件里輸入然後運行該程序,或改源代碼用程序輸入。
**********************/
#include <stdio.h>
#include <string.h>
/***學生信息******/
typedef struct student
{
char *pStrName;
char *pStrSex;
int age;
struct student *next;
}STD;
/***將信息轉成學生結構體****/
STD* setStudent(char array[], int len, int age)
{
STD *p = NULL;
char chName[255];
char chSex[50];
char chAge[50];
int tage;
int index;
int tem;
/***分離名字*****/
for (index = 0; index < len && array[index] != ' '; index++)
{
chName[index] = array[index];
}
chName[index] = '\0';
/*****去掉空格*******/
for (; index < len && array[index] == ' '; index++);
/***分離性別*****/
for (tem = 0; index < len && array[index] != ' ';)
{
chSex[tem++] = array[index++];
}
chSex[tem] = '\0';
for (; index < len && array[index] == ' '; index++);
/***分離年齡*****/
for (tem = 0; index < len && array[index] != ' ';)
{
chAge[tem++] = array[index++];
}
chAge[tem] = '\0';
tage = strtol(chAge, NULL, 10);/****字元到數字的轉換(庫函數)*****/
/*****如果為你選要顯示的年齡則生成學生結點並置相應值*****/
if (tage == age)
{
p = (STD*)malloc(sizeof(STD));/****這里是判申請內存是否成功,以下類同*****/
if (p)
{
p->next = NULL;
p->pStrName = (char *)malloc(sizeof(chName) + 1);
if (p->pStrName)
{
strcpy(p->pStrName, chName);
}
else
{
printf("allocate memory(chName) error!\n");
getch();
exit(0);
}
p->pStrSex = (char *)malloc(sizeof(chSex) + 1);
if (p->pStrSex)
{
strcpy(p->pStrSex, chSex);
}
else
{
printf("allocate memory(chSex) error!\n");
getch();
exit(0);
}
p->age = age;
}
else
{
printf("allocate memory(STD) error!\n");
getch();
exit(0);
}
}
return p;
}
/***從文本中讀取信息********/
STD* readFile()
{
STD s;
STD *pt = NULL;
STD *ph = &s;
FILE *pf;
char chBuf[255];
int index = 0;
char chTem = 'c';
int age = 20;/*所要查找的學生年齡*/
/* 存入文件
pf = fopen("testFile.txt", "w");
if (pf == NULL)
{
printf("open file error !\n");
}
else
{
fwrite("name sex age\n", 1, sizeof("name\n sex age\n"), pf);
}
fclose(pf);
*/
/*****讀取文件*****/
pf = fopen("testFile.txt", "r"); /***默認路徑是源文件所在文件****/
if (pf == NULL)
{
printf("open file error !\n");
exit(0);
}
else
{
while (!feof(pf))
{
chTem = fgetc(pf);
chBuf[index++] = chTem;
if (chTem == '\n')
{
chBuf[--index] = '\0';
pt = setStudent(chBuf, index, age);
if (pt)
{
ph->next = pt;
ph = pt;
}
index = 0;
chBuf[index] = '\0';
}
}
fclose(pf);
}
ph = s.next;
return ph;
}
/*****輸出選出的學生信息*******/
void displayStu(STD *p)
{
printf("\n*********selected student information***********\n");
printf(" name\tsex\tage\n");
while (p)
{
printf("%s %s %d\n", p->pStrName, p->pStrSex, p->age);
p = p->next;
}
printf("\n---------end of display--------\n");
}
void main()
{
STD *p = NULL;
p = readFile();
displayStu(p);
getch();
}
/*已上通過上機*/
/*更多有關C語言的請到我的網路空間一瞧,互相學習*/
/*最近才開始覺得應該把一些東西記在那裡,那裡找不到的可以和我說,看看我能不能幫上什麼忙*/
/*還有做了個九宮圖的程序,可惜還沒完成.完成我再放上去*/
/**http://hi..com/毛求香/ihome/myblog**/
3. C語言 printf和scanf的實現
/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
__printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
int _scanf(char (*get)(void), void (*unget)(char), CONST char *fmt, va_list va)
{
int is_long, c, base;
char *vp;
char s[MAX+1];
int converted = 0;
while (c = *fmt++)
{
if (c == '%')
{
if (*fmt == 'l')
{
is_long = 1;
fmt++;
}
else
is_long = 0;
vp = va_arg(va, void *);
switch (*fmt)
{
case 'c':
*(char *)vp = get();
converted++;
break;
case 'o': base = 8; goto read_strtoul;
case 'u': base = 10; goto read_strtoul;
case 'X':
case 'x': base = 16;
read_strtoul:
converted++;
ReadInteger(s, get, unget, base);
if (is_long)
*(unsigned long *)vp = strtoul(s, 0, base);
else
*(unsigned *)vp = strtoul(s, 0, base);
break;
case 'd':
converted++;
ReadInteger(s, get, unget, 10);
if (is_long)
*(long *)vp = strtol(s, 0, 10);
else
*(int *)vp = strtol(s, 0, 10);
break;
case 's':
converted++;
ReadString(vp, get, unget);
break;
default:
puts("unsupported format");
break;
}
fmt++;
}
else if (isspace(c))
{
while ((c = get()) && isspace(c))
;
unget(c);
}
else if (get() != c)
break;
}
return converted;
}