![](/img/trans.png)
[英]Reading a file with numbers separated by commas and storing each line into a struct pointer
[英]Reading a string of numbers separated by commas
我正在編寫一個函數,它應該讀取一串數字,用逗號分隔。 字符串的格式如下:
"1, 2, 3"
唯一的“規則”是該函數將容忍任何空格或制表符,只要每個數字之間有一個逗號。
如果字符串有效,則將數字存儲在鏈表中。
例如,以下字符串是有效的:
"1,2,14,2,80"
" 250 , 1, 88"
但以下內容無效:
" 5, 1, 3 ,"
"51, 60, 5,,9"
我首先用 strtok() 試試運氣(使用分隔符“,\t”,但據我目前的理解,不可能檢查錯誤。所以我編寫了自己的函數,但我對它非常不滿意 -我認為代碼很糟糕,雖然它似乎可以工作,但我真的很想知道是否有一種更清潔、更簡單的方法來實現這樣的功能。
我的功能是:
void sliceNumbers(char * string)
{
/*flag which marks if we're expecting a comma or not*/
int comma = FALSE;
/*Are we inside a number?*/
int nFlag = TRUE;
/*error flag*/
int error = FALSE;
/*pointer to string start*/
char * pStart = string;
/*pointer to string end*/
char * pEnd = pStart;
/*if received string is null*/
if (!string)
{
/*add error and exit function*/
printf("You must specify numbers");
return;
}
/*this loop checks if all characters in the string are legal*/
while (*pStart != '\0')
{
if ((isdigit(*pStart)) || (*pStart == ',') || (*pStart == ' ') || (*pStart == '\t'))
{
pStart++;
}
else
{
char tmp[2];
tmp[0] = *pStart;
tmp[1] = 0;
printf("Invalid character");
error = TRUE;
pStart++;
}
}
if (!error)
{
pStart = string;
if (*pStart == ',')
{
printf("Cannot start data list with a comma");
return;
}
pEnd = pStart;
while (*pEnd != '\0')
{
if (comma)
{
if (*pEnd == ',')
{
if (!nFlag)
{
}
if (*(pEnd + 1) == '\0')
{
printf("Too many commas");
return;
}
*pEnd = '\0';
/*Add the number to the linked list*/
addNumber(pStart, line, DC);
comma = FALSE;
nFlag = FALSE;
pStart = pEnd;
pStart++;
pEnd = pStart;
}
else if (isdigit(*pEnd))
{
if (!nFlag)
{
printf("numbers must be seperated by commas");
pEnd++;
}
else
{
if (*(pEnd + 1) == '\0')
{
pEnd++;
/*Add the number to the linked list*/
addNumber(pStart);
comma = FALSE;
nFlag = FALSE;
pStart = pEnd;
pStart++;
pEnd = pStart;
}
else
{
pEnd++;
}
}
}
else if (*pEnd == '\0')
{
if (nFlag)
{
/*Add the number to the linked list*/
addNumber(pStart, line, DC);
}
else
{
printf("Too many commas");
}
}
else if (*pEnd == ' ' || *pEnd == '\t')
{
nFlag = FALSE;
pEnd++;
}
}
else
{
if (*pEnd == ',')
{
printf("There must be only 1 comma between numbers");
return;
}
else if (isdigit(*pEnd))
{
if (*(pEnd + 1) == '\0')
{
pEnd++;
/*Add the number to the linked list*/
addnumber(pStart, line, DC);
comma = FALSE;
nFlag = FALSE;
pStart = pEnd;
pStart++;
pEnd = pStart;
}
else
{
pStart = pEnd;
pEnd++;
nFlag = TRUE;
comma = TRUE;
}
}
else if (*pEnd == ' ' || *pEnd == '\t')
{
if (!nFlag)
{
pEnd++;
}
else
{
pEnd++;
}
}
}
}
}
}
您已經定義了許多布爾值(盡管您已將它們聲明為int
s)來跟蹤當前狀態。 您可以將它們組合成一個state
變量,使用#define
定義可能的值:
#define STATE_START 0
#define STATE_IN_NUMBER 1
#define STATE_COMMA 2
#define STATE_FINISHED 3
#define STATE_ERROR 4
int state = STATE_START;
你可以畫一張圖(有點像流程圖),展示每個角色如何將我們從一種狀態轉移到另一種狀態。
(對於我的圖像,我保持簡單,只顯示輸入的非錯誤狀態,沒有空格)
或者簡單地說:
current state | input | next state| side effect
-----------------------------------------------------------------------
START | digit | IN_NUMBER | start storing a number
START | other | ERROR |
IN_NUMBER | digit | IN_NUMBER | continue storing a number
IN_NUMBER | comma | COMMA | complete storing a number
IN_NUMBER | null | FINISHED | finalise output
IN_NUMBER | other | ERROR | report error
COMMA | digit | IN_NUMBER | start storing a number
COMMA | comma | ERROR |
COMMA | other | ERROR |
(對於我的表,我添加了基本的錯誤狀態,但仍然沒有考慮空格)
您將需要添加更多狀態和轉換來處理空格和制表符,但原則不會改變。 我建議從一個沒有空格的實現開始,然后添加它。
這允許您編寫一個有限狀態機,它的一個實現如下所示:
int state = STATE_START;
while(state != STATE_FINISHED && state != STATE_ERROR) {
char c = input[offset++];
switch(state) {
case STATE_START:
state = handleStateStart(...);
break;
case STATE_IN_NUMBER:
state = handleInNumber(...);
break;
// etc.
default:
sprintf(error_message, "Reached unsupported state: %c", state);
state = STATE_ERROR;
}
}
處理函數的參數需要傳入它將讀取和修改的數據結構。 例如:
int handleStateStart(
char c,
int* current_number,
char *error_message)
{
if( ! isDigit(c)) {
sprintf(error_message, "Expected a digit at char %d", *offset);
return STATE_ERROR;
}
*current_number = atoi(c);
return STATE_IN_NUMBER;
}
(這是一種易於理解的狀態機實現方式,但還有其他方式可以做到: 有沒有典型的狀態機實現模式? )
您的 CSV 解析問題非常適合狀態機,生成的代碼將非常整潔。 狀態機用於更復雜的解析任務,並大量用於編譯器等。 稍后在您的學習中,您將遇到正則表達式——正式地,正則表達式是一種表達消耗字符的有限狀態機的緊湊方式。
strtok()
是正確的方法。 但僅通過","
(逗號)作為分隔符。 您可以檢查結果字符串的長度為零( strlen(tok)==0
),這意味着您有兩個連續','
。 檢查后,您只需修剪結果,即如此處所述。
您可以使用正則表達式庫
1) 驗證字符串
[^\d, ]|,[[:blank:]]+,|,{2,}
在哪里
[^\d, ]
- 找到除數字、逗號和空格之外的所有符號
,[[:blank:]]+,|,{2,}
- 驗證字符串 2 個或多個帶有空格和制表符的逗號,逗號之間沒有數字
2) 進程號
\d+
你可以在這里在線嘗試
一種非常有效的直接方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.