我想編輯一個文本文件,但我一直在尋找正確的功能或方法。 到目前爲止,我可以打開一個文本文件並查找某個Sting,但我不知道如何移動光標,添加或替換信息,下面顯示的僞代碼中的步驟4 - 7。編輯文本文件
你能提供一些指導嗎?我應該使用哪些功能(如果它們已經存在)? 一個樣本'簡單'的代碼也將不勝感激。
Pseudocode:
1. Open file.
2. While not eof
3. Read file until string "someString" is found.
4. Position the cursor at the next line (to where the someString was found).
5. If "someString" = A go to step 6. Else go to step 7.
6. Replace the information in whole line with "newString". Go to step 8.
7. Add new information "newString_2", without deleting the existing.
8. Save and close the text file.
謝謝。
文件沒有像這樣的「遊標」。你是在談論編寫一個* visual *文本編輯器,或者你是在談論從文件中讀取文本,然後寫出它的新版本? – crashmstr
你不能直接在文件中替換一行,文件不會做出這樣的區分。我的建議是,您閱讀文件並將所需的輸出寫入新文件。 – Havenard
@Havenard這將是更簡單的解決方案,但您可以通過刪除內容並插入新內容來「替換一行」。 –