导航:首页 > 源码编译 > strtol源码

strtol源码

发布时间:2023-02-22 04:03:16

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;
}

阅读全文

与strtol源码相关的资料

热点内容
吃冰长视频声控解压 浏览:670
社交加密保护 浏览:14
程序员那么可爱求婚地点 浏览:271
phpredis原子操作 浏览:211
怎么把系统默认文件夹改名 浏览:203
遗传算法中如何实现最大最小 浏览:493
工厂里程序员做什么 浏览:996
hibernatelinux 浏览:871
帮人解压需要钱吗 浏览:232
怎么给微信加密码锁指纹 浏览:931
app图片不能下载怎么采集 浏览:769
如何在服务器上安装应用 浏览:696
网上国网app在哪里看到已推广 浏览:826
app同步到云端怎么设置 浏览:986
陕西养老app如何帮其他人缴费 浏览:170
编译器不检查源程序的正确与否 浏览:964
微信扫码收付款怎么弄加密 浏览:674
虚拟服务器如何造成内网ip冲突 浏览:211
plc有那些编程软件 浏览:863
web项目怎么部署大服务器 浏览:866