C language Atof,atoi, strtok and other functions usage

Source: Internet
Author: User
Tags strcmp strtok

Atoi is to convert a string to int type data

Atof is converted to float type

Strtok are delimited strings.


The first example uses SSCANF, which is not used Strtok

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define  max _line 2048int main () {    file *fr;    fr =  fopen ("Test_group_box.txt",  "R");     if (NULL&NBSP;==&NBSP;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); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SSCANF (test_n_arr,  "%s\t%s\t%s", &NBSP;STR1,&NBSP;STR2 , &NBSP;STR3);         t = atof (STR3);         printf ("%s\t",  str1);         printf ("%s\t ", &NBSP;STR2);        if  (strcmp (str1," Group ")  == 0) {             printf ("%s\n", &NBSP;STR3);         }else {             printf ("%.2f\n",  t);        }     }    fclose (Fr);     // free (Test_n_arr);     return 0;} 


Another example uses Strtok.

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define  max _line 2048int main () {    file *fr;    fr =  fopen ("Test_group_box.txt",  "R");     if (NULL&NBSP;==&NBSP;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,&NBSP;MAX_LINE,&NBSP;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;}



C language Atof,atoi, strtok and other functions usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.