2014-01-28 35 views
-3

能有人給我介紹瞭如何在這個順序讀取文本文件:讀取C文件,並找到子串

Line of text here 
number1(startposition) 
number2(endposition) 
Line of text here 
number1(startposition) 
number2(endposition) 
Line of text here 
number1(startposition) 
number2(endposition) 

,然後用數字1作爲指定startPosition和NUMBER2爲找到文本行的子endposition並將子字符串複製到新字符串中。

+0

他們可能可以,但堆棧溢出的工作方式是,當你嘗試某些東西,然後在遇到磚牆時詢問具體問題。 – John3136

回答

2

OK,這裏是簡介:

  • 在緩衝區中使用fgetsstr
    • 讀2線
    • 使用strtol
    • 檢查轉換的2線數字讀取一行索引有意義(即它們在字符串中)
    • 分配一個新字符串length = end - start + 1
    • 使用memcpystr + start
    • 複製end - start字節確保一個0終止添加到您的目標字符串
  • 回到第一步

在每一點當你做一個呼叫準備好讓它失敗。

+0

這種情況下不包括字符的結束位置。 – BLUEPIXY

+0

@BLUEPIXY事實上,OP應該根據他/她自己對「endposition」的解釋來調整它。 – cnicutar