一些C函數的用法(筆記)

(1)fscanf sscanf

    FILE *in = fopen ("/proc/uptime", "r");
    long uptim = 0, a, b;
    if (in)
      {
   if (2 == fscanf (in, "%ld.%ld", &a, &b))
     uptim = a * 100 + b;
   fclose (in);
      }

------------------------------------------------------------------------------------------------------------------------------

  file = fopen("/proc/stat", "r");

    if (file == 0)
    {
        printf("file:/proc/stat not exist/n");
        return ;
    }

    char line[512];

    // 獲取相關數據
    while (line == fgets(line, 512, file))
    {
        if (4 == sscanf(line, "cpu  %lu %lu %lu %lu", &cpuInfo.CpuUser,&cpuInfo.CpuNice,
                        &cpuInfo.CpuSystem,&cpuInfo.CpuIdel))
        {
            break;
        }
    }

(2)strtok 與strtok_
我們有一段字符串 "Fred male 25,John male 62,Anna female 16" 我們希望把這個字符串整理輸入到一個struct,

 
struct person {
    char [25] name ;
    char [6] sex;
    char [4] age;
}
#include<stdio.h>
#include<string.h>
#define INFO_MAX_SZ 255
int main()
{
  int in=0;
  char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";
  char *p[20];
  char *buf=buffer;

  while((p[in]=strtok(buf,","))!=NULL) {
            buf=p[in];
            while((p[in]=strtok(buf," "))!=NULL) {
                      in++;
                      buf=NULL;
                   }
                p[in++]="***"; //表現分割
                buf=NULL; }

  printf("Here we have %d strings/n",i);
  for (int j=0; j<in; j++)
        printf(">%s</n",p[j]);
  return 0;
}
這個程序輸出爲:
Here we have 4 strings
>Fred<
>male<
>25<
>***<
這只是一小段的數據,並不是我們需要的。但這是爲什麼呢? 這是因爲strtok使用一個static(靜態)指針來操作數據,讓我來分析一下以上代碼的運行過程:

紅色爲strtok的內置指針指向的位置藍色爲strtok對字符串的修改

1. "Fred male 25,John male 62,Anna female 16" //外循環

2. "Fred male 25/0John male 62,Anna female 16" //進入內循環

3.   "Fred/0male 25/0John male 62,Anna female 16"

4.   "Fred/0male/025/0John male 62,Anna female 16"

5 "Fred/0male/025/0John male 62,Anna female 16" //內循環遇到"/0"回到外循環

6  "Fred/0male/025/0John male 62,Anna female 16" //外循環遇到"/0"運行結束。

3. 使用strtok_r
在這種情況我們應該使用strtok_r, strtok reentrant.
char *strtok_r(char *s, const char *delim, char **ptrptr);

相對strtok我們需要爲strtok提供一個指針來操作,而不是像strtok使用配套的指針。
代碼:
 
#include<stdio.h>
#include<string.h>
#define INFO_MAX_SZ 255
int main()
{
  int in=0;
  char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";
  char *p[20];
  char *buf=buffer;

  char *outer_ptr=NULL;
  char *inner_ptr=NULL;

  while((p[in]=strtok_r(buf,",",&outer_ptr))!=NULL) {
            buf=p[in];
            while((p[in]=strtok_r(buf," ",&inner_ptr))!=NULL) {
                      in++;
                      buf=NULL;
                   }
                p[in++]="***";
                buf=NULL; }

  printf("Here we have %d strings/n",i);
  for (int j=0; jn<i; j++)
        printf(">%s</n",p[j]);
  return 0;
}

這一次的輸出爲:
Here we have 12 strings
>Fred<
>male<
>25<
>***<
>John<
>male<
>62<
>***<
>Anna<
>female<
>16<
>***<


讓我來分析一下以上代碼的運行過程:

紅色爲strtok_r的outer_ptr指向的位置
綠色爲strtok_r的inner_ptr指向的位置,
藍色爲strtok對字符串的修改

1. "Fred male 25,John male 62,Anna female 16" //外循環

2. "Fred male 25/0John male 62,Anna female 16"//進入內循環

3.  "Fred/0male 25/0John male 62,Anna female 16"

4  "Fred/0male/025/0John male 62,Anna female 16"

5 "Fred/0male/025/0John male 62,Anna female 16" //內循環遇到"/0"回到外循環

6  "Fred/0male/025/0John male 62/0Anna female 16"//進入內循環
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章