int safegets(char *buffer,int n){
char *ptr; /** ptr used for finding '\n' **/
buffer[0] = '\0';
fgets(buffer,n,stdin); /** Get n chars **/
buffer[n+1]='\0'; /** Set last char to null **/
ptr=strchr(buffer,'\n'); /** If string contains '\n' **/
if (ptr!=NULL){ /** If string contains '\n' **/
ptr[0]='\0'; /** Change char to '\0' **/
if (buffer[0] == '\0') printf("\n ... no entry ...\n");
}
return strlen(buffer); /** Return the length **/
运行结果:
函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:
double atof (const char* str);
atof() 的名字来源于 ascii to floating point numbers 的缩写,它会扫描参数str字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数str 字符串可包含正负号、小数点或E(e)来表示指数部分,如123. 456 或123e-2。
【返回值】返回转换后的浮点数;如果字符串 str 不能被转换为 double,那么返回 0.0。
温馨提示:ANSI C 规范定义了 stof()、 atoi()、 atol()、 strtod()、 strtol()、 strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习;使用 atof() 与使用 strtod(str, NULL) 结果相同。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。
范例:
执行结果:
a = -100.23
b = 2.00
c = 341.00
d = 100.34
e = 0.00
char *ptr; /** ptr used for finding '\n' **/
buffer[0] = '\0';
fgets(buffer,n,stdin); /** Get n chars **/
buffer[n+1]='\0'; /** Set last char to null **/
ptr=strchr(buffer,'\n'); /** If string contains '\n' **/
if (ptr!=NULL){ /** If string contains '\n' **/
ptr[0]='\0'; /** Change char to '\0' **/
if (buffer[0] == '\0') printf("\n ... no entry ...\n");
}
return strlen(buffer); /** Return the length **/
}
以上,为字符串读入程序。
atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
atoi()函数实现的代码:
- /*
- * name:xif
- * coder:xifan@2010@yahoo.cn
- * time:08.20.2012
- * file_name:my_atoi.c
- * function:int my_atoi(char* pstr)
- */
- int my_atoi(char* pstr)
- {
- int Ret_Integer = 0;
- int Integer_sign = 1;
- /*
- * 判断指针是否为空
- */
- if(pstr == NULL)
- {
- printf("Pointer is NULL\n");
- return 0;
- }
- /*
- * 跳过前面的空格字符
- */
- while(isspace(*pstr) == 0)
- {
- pstr++;
- }
- /*
- * 判断正负号
- * 如果是正号,指针指向下一个字符
- * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
- */
- if(*pstr == '-')
- {
- Integer_sign = -1;
- }
- if(*pstr == '-' || *pstr == '+')
- {
- pstr++;
- }
- /*
- * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
- */
- while(*pstr >= '0' && *pstr <= '9')
- {
- Ret_Integer = Ret_Integer * 10 + *pstr - '0';
- pstr++;
- }
- Ret_Integer = Integer_sign * Ret_Integer;
- return Ret_Integer;
- }
现在贴出运行my_atoi()的结果,定义的主函数为:int main ()
- int main()
- {
- char a[] = "-100";
- char b[] = "456";
- int c = 0;
- int my_atoi(char*);
- c = atoi(a) + atoi(b);
- printf("atoi(a)=%d\n",atoi(a));
- printf("atoi(b)=%d\n",atoi(b));
- printf("c = %d\n",c);
- return 0;
- }
运行结果:
头文件:#include <stdlib.h>
函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:
double atof (const char* str);
atof() 的名字来源于 ascii to floating point numbers 的缩写,它会扫描参数str字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数str 字符串可包含正负号、小数点或E(e)来表示指数部分,如123. 456 或123e-2。
【返回值】返回转换后的浮点数;如果字符串 str 不能被转换为 double,那么返回 0.0。
温馨提示:ANSI C 规范定义了 stof()、 atoi()、 atol()、 strtod()、 strtol()、 strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习;使用 atof() 与使用 strtod(str, NULL) 结果相同。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。
范例:
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- char *a = "-100.23",
- *b = "200e-2",
- *c = "341",
- *d = "100.34cyuyan",
- *e = "cyuyan";
- printf("a = %.2f\n", atof(a));
- printf("b = %.2f\n", atof(b));
- printf("c = %.2f\n", atof(c));
- printf("d = %.2f\n", atof(d));
- printf("e = %.2f\n", atof(e));
- system("pause");
- return 0;
- }
a = -100.23
b = 2.00
c = 341.00
d = 100.34
e = 0.00