簡體   English   中英

使用while循環將Strtok存儲到多個變量

[英]Strtok store to multiple variable using while loop

void split(char *pt){
     char *words;

     words = strtok(pt, ",")
     while(words != NULL){
          strcpy(Var[in].Name,words); //Store data here
          words = strtok(NULL, ",");

          Var[in].Year = atoi(words)); //Store data here
          words = strtok(NULL, ",");

          Var[in].Grades = atoi(words)); //Store data here
          words = strtok(NULL, ",");

     }
}

如何將每個單詞的所有標記化單詞存儲在 Array Var 中以單獨顯示。 所有變量都在這個結構中。

struct student{
     char Name[40]; //Pass the data here
     int Year; //Pass the data here
     float Grades; //Pass the data here

     //Then later display function will show all the results
}

當我運行程序時,只有文本文件的最后一個值(成績)填充了所有行。 而且我嘗試不使用while循環,我得到了預期的結果,但問題是程序不會關閉。 我正在使用 switch 語句。

//Example words in the text file
John Doe,2,79.80,
Jane Doe,2,89.70,

//Expected Result
Name       | Year | Grades 
John Doe   |  2   | 79.80
Jane Doe   |  2   | 89.70    
void split(char *pt){
     char *words;

     words = strtok(pt, ",")
     while(words != NULL){
          strcpy(Var[in].Name,words); //Store data here
          words = strtok(NULL, ",");

          Var[in].Year = atoi(words)); //Store data here
          words = strtok(NULL, ",");

          Var[in].Grades = atoi(words)); //Store data here
          words = strtok(NULL, ",");

     }
}

如何將每個單詞的所有標記化單詞存儲在 Array Var 中以單獨顯示。 所有變量都在這個結構中。

struct student{
     char Name[40]; //Pass the data here
     int Year; //Pass the data here
     float Grades; //Pass the data here

     //Then later display function will show all the results
}

當我運行程序時,只有文本文件的最后一個值(成績)填充了所有行。 而且我嘗試不使用while循環,我得到了預期的結果,但問題是程序不會關閉。 我正在使用 switch 語句。

//Example words in the text file
John Doe,2,79.80,
Jane Doe,2,89.70,

//Expected Result
Name       | Year | Grades 
John Doe   |  2   | 79.80
Jane Doe   |  2   | 89.70    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM