c语言atof字母,C语言atof,atoi ,strtok等函数用法

atoi是把字符串转换为int型数据

atof是转换为float型

strtok是分隔字符串的

第一个例子用了sscanf, 没有用strtok#include

#include

#include

#include

#define MAX_Line 2048

int main(){

FILE *Fr;

Fr = fopen("test_Group_Box.txt", "r");

if(NULL == Fr){

printf("ERROR!\n");

return -1;

}

float t;

char Test_N_arr[MAX_Line];

//  char *Test_N_arr = (char*)malloc(sizeof(char) *MAX_Line);

char str1[50], str2[50], str3[50];

//  char str1, str2, str3;

//  "Read Each Line of Fr and assign each fields to three pointers"

while (!feof(Fr)) {

fgets(Test_N_arr, MAX_Line, Fr);

sscanf(Test_N_arr, "%s\t%s\t%s", str1, str2, str3);

t = atof(str3);

printf("%s\t", str1);

printf("%s\t", str2);

if (strcmp(str1,"Group") == 0){

printf("%s\n", str3);

}else {

printf("%.2f\n", t);

}

}

fclose(Fr);

// free(Test_N_arr);

return 0;

}

另一个例子用了strtok#include

#include

#include

#include

#define MAX_Line 2048

int main(){

FILE *Fr;

Fr = fopen("test_Group_Box.txt", "r");

if(NULL == Fr){

printf("ERROR!\n");

return -1;

}

float t;

char *Test_N_arr = (char*)malloc(sizeof(char) *MAX_Line);

char *str;

//  "Read Each Line of Fr and assign each fields to three pointers"

while (!feof(Fr)) {

fgets(Test_N_arr, MAX_Line, Fr);

str = strtok(Test_N_arr, "\t");

int i = 0;

while(str != NULL){

if( i == 2){

if(strcmp(str, "Qual\n") == 0){

printf("%s", str);

} else {

t = atof(str);

printf("%.2f\n", t);

}

}else {

printf("%s\t", str);

}

i++;

str = strtok(NULL, "\t");

}

}

fclose(Fr);

free(Test_N_arr);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值