我正在嘗試編寫一個接受MadLib大綱作爲.txt文件的C程序。該程序然後要求用戶輸入一系列短語放入Madlib中。我有這部分完整。但是,我希望能夠將用戶輸入的MadLib短語&插入到早先打開的.txt文件中。然後,我希望能夠允許用戶將完成的MadLib保存在不同的.txt文件名下。 MadLib大綱在其中放置了關鍵字,表示用戶輸入的單詞應該放在哪裏。 (見下文)。帶重定向輸入和輸出的MadLib C程序
我應該如何去用這些用戶輸入的短語替換這些佔位符?
馬德利布大綱.txt文件:
One of the most <adjective> characters in fiction is named
"Tarzan of the <plural-noun>." Tarzan was raised by a/an
<noun> and lives in the <adjective> jungle in the
heart of darkest <place>. He spends most of his time
eating <plural-noun> and swinging from tree to <noun>.
Whenever he gets angry, he beats on his chest and says,
"<funny-noise>!" This is his war cry. Tarzan always dresses in
<adjective> shorts made from the skin of a/an <noun>
and his best friend is a/an <adjective> chimpanzee named
Cheetah. He is supposed to be able to speak to elephants and
<plural-noun>. In the movies, Tarzan is played by <person's-name>.
您可以從'fgets'開始從您的文件中讀取一行,然後使用'strchr'來查找尖括號。然後將文本複製出來,並將尖括號中的文本替換爲您閱讀的文字。我不確定除了編寫程序來完成您所描述的內容之外,還有別的方法可以回答您的問題。如果您有更具體的問題,請告訴我。 – yellowantphil 2014-11-06 01:15:50