Kuberentes 支援的運算資源種類繁多,諸如 Pod, Job, ReplicaSet, DaemonSet, Deployment 以及 StatefulSet.
其中 StatefulSet 則是特別針對 stateful 應用程式所設計的,實際使用上會有些許小地方與 Deployment 用法不同,譬如
1. Storage 使用的是 Template 的概念,希望每個 StatefulSet Pod 可以綁定固定的 PV
2. 名稱採取流水號設計,相對於 Deployment -> ReplicaSet -> Pod 兩層亂數來說, StatefulSet 的名稱更加顯眼
3. StatefulSet 的 Pod 有網路存取唯一性,搭配 Headless Service,就可以使用固定一組的 DNS 名稱來存取固定 Pod(預設情況下,Pod 重啟 IP 就會不同,要做到固定一致需要不少修改)
今天這篇文章算是一個入門教學文,主要是跟大家探討什麼是 StatefulSet,以及於 Kubernetes 內該如何使用,使用上有什麼要注意的,以下幫大家節錄部分資訊
1. StatefulSet 的設計是安全優先,所以預設情況下刪除 StatefulSet 不會連帶刪除使用的 Volume
2. 使用 StatefulSet 的時候,要記得處理當 Pod 被停止,被刪除時,相關資料的寫入與同步
3. 預設情況下, StatefulSet 的創建與移除都是一個一個來,創建時由 0-N-1, 移除則反過來。
4. 對於多節點的 StatefulSet 來說,這些應用程式可能都會需要一些初始化的操作,甚至 leader-election 等相關算法。這意味者 Container(s) Running 不等於 Pod 以及準備好了。
5. 針對上述概念,使用者一定要仔細的去設定 liveness 以及 readiness,來確保
6. PostStart/PreStop 的用法與時機也要考慮進去,特別是 PreStop 用得好還可以減少使用端封包無故重送
7. 研究 disruption budgets 的用法,來確保你的 StatefulSet 運行得更安心
8. 針對特定的儲存空間使用權限問題,可以考慮使用 fsGroup 來修改,而不用自己寫一堆 shellScript 來做。當然要注意儲存空間檔案過多內容過大的情況,預設的 fsGroup 行為可以會導致你的 Pod 花太多時間來啟動
原文: https://itnext.io/stateful-applications-in-kubernetes-808a60bc109
想學習更多關於 Kubernetes 的實戰經驗與基礎設計,可參閱我的線上課程: https://technologynoteniu.github.io/awesome-notes/linux/network/
同時也有10000部Youtube影片,追蹤數超過2,910的網紅コバにゃんチャンネル,也在其Youtube影片中提到,...
「for while 使用 時機」的推薦目錄:
- 關於for while 使用 時機 在 矽谷牛的耕田筆記 Facebook 的精選貼文
- 關於for while 使用 時機 在 當張仲景遇上史丹佛 Facebook 的精選貼文
- 關於for while 使用 時機 在 コバにゃんチャンネル Youtube 的最佳解答
- 關於for while 使用 時機 在 大象中醫 Youtube 的最佳貼文
- 關於for while 使用 時機 在 大象中醫 Youtube 的最讚貼文
- 關於for while 使用 時機 在 [問題] 關於fflush的使用時機- 看板C_and_CPP - 批踢踢實業坊 的評價
- 關於for while 使用 時機 在 Java學習筆記-迴圈式 的評價
- 關於for while 使用 時機 在 C++ 教學入門- do while 基本語法與使用時機 - YouTube 的評價
for while 使用 時機 在 當張仲景遇上史丹佛 Facebook 的精選貼文
從非典到新冠肺炎
From SARS to Novel Coronavirus (COVID-19) - English version is in the second half
新冠肺炎,Novel coronavirus (2019-nCoV) 新型冠状病毒肺炎,疫情越來越嚴重,被證實可以人傳人,也在武漢以外的許多地方發現病例,造成世界各國及世界衛生組織(WHO)高度關切,當然也讓很多住在中國大陸及附近區域的華人非常緊張,就好像當年的非典肺炎疫情即將再一次大爆發一樣。
目前,現代醫學還沒有找到治療新冠肺炎的方法,只能針對某些症狀來處理,疫苗的研發更是遙遙無期。怎麼辦?每次遇到這種情況,中醫就會被搬出來,這次也不例外。新冠肺炎爆發後,網上馬上有許多中醫對付新冠肺炎的文章。當然,除非哪位中醫師看過、治好過大量的新冠肺炎病例,所有的討論都是猜想、假設。然而,有些猜想及假設值得參考,有些猜想及假設卻明顯在誤導大眾。
我還沒有治療過新冠肺炎,不能大肆評論。不過,我治好過很多禽流感、豬流感、及每年流感導致的嚴重肺炎及其它病變的病人。其中許多病人是被美國大型西醫院證實為嚴重肺炎,被要求醫院或居家隔離,偷偷溜出來找中醫看診的。也因此許多病人及討論中醫的網站,希望我能針對新冠肺炎發表一些意見。
我們以前就討論過,這些彪悍的流感病毒,經過那麼多年、那麼多次的變種(mutation),每年都不一樣,東漢時期傳下來的經典中醫,根本沒有遇到過現在的病毒,怎麼可能治療如此嚴重的肺炎呢?
中醫從來就不認識病毒,也不從病毒種類的角度來思考。中醫是探討人體受到外界因素破壞,失去平衡後,身體會有哪些現象、哪些反應,根據那些現象、那些反應來調整身體狀況,期待身體能恢復到平衡狀態,把外界因素帶來的破壞減到最小。我打個半開玩笑的比方,警匪槍戰時,我們注意到壞人哪個方向來的火力強大,造成我們部署在哪個位置的警員傷亡,這時我們會趕緊重新部署人員,或者想辦法增派警力,我們大概無暇去管壞人是用哪個牌子的槍、哪個工廠做的子彈!
雖然幾百年幾千年下來,病毒變種等等的外界因素改變了非常多,人體演化的改變卻非常有限。人體的功能,無論是怎麼被破壞的,某項功能被破壞而導致的症狀、反應、後續演變,卻依然有明顯的脈絡可循。也因為如此,在很多情況下,中醫以專注人體本身平衡狀態的治療方式,反而比西醫專注在外來敵人的治療方式來得有效許多。
依據多年累積大量的臨床病例觀察,無論是禽流感、豬流感、還是每年的流感,人體敗壞的進程依然如同傷寒雜病論探討的一樣,非常簡化的說,從一般桂枝湯證、葛根湯證等的表寒,轉變到小青龍湯證等的裡寒,津液不足、水道運化失調而化熱,變成比較嚴重的大青龍湯證,或者更嚴重肺臟的寒熱夾雜,金匮要略肺痿肺癰咳嗽上氣病脈證治第七篇中的射干麻黃證、葶藶大棗瀉肺湯證、澤漆湯證、小青龍加石膏湯證等等混雜出現,搞得亂七八糟,也不再是什麼簡單方劑可以對應的。
然而,雖然進程很像,不同於一般外感的是,這些嚴重流感肺炎病情加重的改變速度快非常多,也來得猛烈頑強很多。一般的外感從桂枝湯證、葛根湯證等轉變到大青龍湯證或更複雜的病情,通常需要一兩週的時間。同時,還得病人自己非常不注意,或者醫生治療錯誤,一般感冒才會沒辦法自己好,反而變成嚴重的病症。這幾年的流感,從一開始覺得不太對勁,到嚴重複雜的病情,只需要三四天,而且有越來越快的趨勢。這大幅提高中醫師治療流感時,判斷功力及敏感度的要求,中醫師必須在許多症狀還沒有出現時,就得抓緊時間,趕緊行動,卻又不能預防過度,反而讓病情加重。換句話說,時機、劑量、藥材比例變得非常重要,稍有不慎,就無法反轉病情。
舉個例子,有些病人得了流感,咳嗽非常嚴重,痰非常多,呼吸困難。依照中醫的辨證,假如一致都是寒,舌苔白、小便清、怕冷等等,本來依照辨證論治,我們可能會開射干麻黃湯加減給病人。然而,因為流感的進程非常快速,中醫師得非常敏感,譬如看到舌苔白卻帶有一絲絲乾的感覺,就很可能得加上大寒的石膏來避免肺喪失津液,卻又不能加太多石膏,以免肺寒加重。又譬如聽到咳嗽聲音非常深沈,從肺的底部發出,又帶有膿痰的濁音,就很可能得加上瀉肺的葶藶來避免肺中水飲、痰飲大幅增加,卻又不能加太多葶藶,以免肺變得太虛弱。
我們回頭來看這次的新冠肺炎。根據有限的資訊,我們知道感染後有大約兩週的潛伏期,這段時間沒有什麼症狀,病人可能只會感到有些疲憊。剛開始發病時,很像一般的感冒,病人會發熱、乏力,並不嚴重,沒有什麼流鼻涕等上呼吸道的症狀,有的甚至沒有發熱。約一半的病人一週多後恢復,另一半的病人卻在一週後出現呼吸困難,有些病人會快速進展為急性呼吸窘迫綜合征、膿毒症休克、代謝性酸中毒、凝血功能障礙等等嚴重的問題,可能導致死亡。
從上面的敘述,我們不難發現,一開始很像一般中醫外感的桂枝湯證、葛根湯證,一半的病人也就自己恢復了,另一半的病人卻出現快速的入裡化熱現象,肺津液迅速流失,非常濃稠的痰飲沈積在肺部下方。同時,中醫認為肺為人體調節津液的源頭,肺金生水,好比天空下雨一般,而當肺的功能及津液調節出現嚴重障礙,很快就會拖累三焦水道、腎臟等的功能,導致上面提到的幾種嚴重病情。換句話說,新冠肺炎可以讓輕微的太陽證外感,迅速發展成嚴重的肺痿肺癰,再進一步瓦解人體其它功能的運作。
怎麼治療?在沒有直接治療武漢肺炎病人的情況下,我們也只能根據有限的資訊來推論,不過,以前大量的流感肺炎治療病例,可以讓我們比較有信心的面對新冠肺炎。當病人已經出現明顯新冠肺炎症狀時,大多已經入裡化熱,嚴重的肺痿肺癰。這個時候,得用大劑量的石膏清肺熱、加強肺津液運作。也得靠葶藶、大戟等把肺下方濃稠的痰飲及胸腔可能的積液去掉,痰飲積液不去,是無法修復肺家津液運作的。同時,肺氣不宣,就好像吸管上頭堵住了,吸管內的水無法上下,我們還得使用麻黃等宣肺、發陽的中藥來配合。另外,肺已經受損了,除了大動作急救外,比較穩定後,還得靠一些潤肺的藥來收尾,讓肺完全恢復。如果我們列一個可能加入的中藥單,大致有石膏、葶藶子、大戟、生半夏、麻黃、射干、紫菀 、款冬花、 生薑、炙甘草、紅棗、麥門冬、杏仁等等。當然,如前面所言,用藥的時機、劑量、藥材比例非常重要,每一個病人的差異也很大,嚴格考驗中醫師的功力與膽識,一旦判斷錯誤,不但沒有效果,反而可能會加重病情。
網上有些中醫師,說新冠肺炎或其它流感肺炎可以用板藍根清熱解毒來治好。也有些中醫師說可以用麥門冬湯等等的輕劑治好嚴重的肺炎。甚至還有些中醫師說多喝綠豆湯可以預防新冠肺炎!其實,真的遇過、治好過禽流感、豬流感等嚴重流感肺炎的中醫師,一看這些文章,就幾乎可以確定這些人根本沒有治療過嚴重肺炎的經驗,充其量只是在西醫治療下,在旁邊幫幫病人一些小忙而已。這樣的情況下,難怪中國政府平時大力推展中醫,真的有如同新冠肺炎這樣重大疫情爆發時,卻看不到中國政府大量使用中醫方法來治療病人、控制疫情。醫學是實戰的學問,沒有大量臨床病例,講得再好聽都是沒有用的,如果希望中醫真的在主流醫學裡站立起來,希望中醫真的能面對大規模的疫情,回歸最基本的臨床療效,才是最重要的,其它都只像是武術表演,而非實際作戰。
From SARS to Novel Coronavirus (COVID-19)
Written in Chinese by Dr. Andy Lee, January 21, 2020
Translated to English by Dr. James Yeh and Dr. Andy Lee, March 28, 2020
The epidemic from Novel Coronavirus is becoming much more serious. Transmissivity among people has been proven. (Note: It's now named COVID–19. The term “Coronavirus” will be used here.) Cases were found in areas beyond Wuhan. It has caused serious attentions from the WHO (World Health Organization) and many countries around the world. The residents in China and the surrounding regions are quite worried and wonder whether it will break out like SARS (2003). (Note: The article was written on January 21, 2020, before Coronavirus became a global pandemic.)
So far, the modern medical field has not found a cure for Coronavirus, but resorts to treating patients’ symptoms only. Any vaccine to treat Coronavirus is still no way in the sight. What do we do? Every time such a situation happens, the topic of using Traditional Chinese Medicine (TCM) is raised (at least among the Chinese communities). There is no exception this time. Many articles related to using TCM on Coronavirus have been popping up on the web. However, unless some TCM doctors who have actually treated many Coronavirus cases, all the discussion would be hypotheses or assumptions. Some hypotheses are worth considering while many others could be quite misleading.
Personally I have not treated patients cases related to Coronavirus. (Note: Shortly after this writing, the author has directly and indirectly participated in treating patients of Coronavirus successfully, and has published other later blogs which included his involvement in treating those patients. Please refer to his medical blog http://www.DrLee.us.) However, I did treat and cure patients inflicted by other viruses in the past, such as the Bird Flu, Swine Flu, and other influenza. A good amount of those patients were diagnosed as severe pneumonia by large hospitals and were required for isolation or self-quarantine. Hence many of patients and online medical forums online are asking for my opinions about Coronavirus.
As we discussed before, all these viruses from the outbreaks are either newly found or mutated from previous strands. The strand can be different every year. Therefore, people always ask how one can say that the TCM knowledge developed in East Han Dynasty (25-220 AD) would be any useful for treating the modern diseases, let alone the severe ones.
It turns out that TCM does not recognize any virus and does not deal with the concept of which type of virus is microscopically at work. TCM looks at how human bodies would become out-of-balance and react to external stimuli. Once the body is out of balance, what symptoms will exhibit and what reactions will be to adjust the body conditions to regain the balance, hence to reduce the damage to the body to the minimum. Let me take an example to illustrate: when there is a gunfight between the police and bandits, we want to see which direction the shots are coming from, causing casualties of the police force, so that we are able to adjust or reinforce the police power. We have no time to think about which brand of the guns or bullets the bandits use.
Over thousands of years, the external viruses have changed and evolved quite a bit, but the evolution of human beings was quite limited. The human body function, no matter how it was damaged, the symptoms due to the damage of the function, the reactions, and the following progression of the disease still follow certain paths. For this very reason, TCM’s focus on the balance of the human body often surpasses the effectiveness of Western medicine, which focuses more on external treats and the microscopic aspect of how human body’s cells are impacted by the external treats.
From the accumulation of many years of clinical treatment and observations, no matter it is Bird Flu, Swine Flu or other influenza, the bodily ‘damage’ and its progression by the viral attack still follow the description of the classic TCM literature “Treatise on Cold Damage on Miscellaneous Disease” (傷寒雜病論). In short summary, the disease usually starts with “Exterior Deficiency or Weakness” (表虛) or “External Coldness” (表寒), for which is matched to one of the several syndromes named with the corresponding herbal remedies such as “Gui Zhi Tang” (桂枝湯) and “Ge Geng Tang” (葛根湯). Then, the disease moves onto the next stage “Interior Coldness” (裡寒) or “Lung Coldness” (肺寒), which shows the syndromes named as “Xiao Qing Long Tang” (小青龍湯), etc. When the respiratory system is “affected by the coldness”, the body fluid function of the respiratory system gets affected. The circulation function of the lung becomes “Dry and Overheated” (燥热). This would lead to a more serious stage “Heated Interior” (入裡化熱) and would often be matched to its herbal remedy “Da Qing Long Tang” (大青龍湯). Or, even worse, it becomes so-called “mixed coldness and heat” (寒熱夾雜) in the lung. Such a complex situation was extensively discussed in Chapter 7 of the classic literature “Synopsis of Prescriptions of the Golden Chamber” (金匮要略肺痿肺癰咳嗽上氣病脈證治第七篇). At this complex stage, the illness development varies significantly among patients of different preconditions and other variants. It is no longer the situation that a simple herbal remedy can be applied to all the situations. The TCM theory illustrates various treatments by those herbal remedies such as “She Gan Ma Hung Tang” (射干麻黃湯), “Ting Li Da Zao Xie Fei Tang” (葶藶大棗瀉肺湯), ”Ze Qi Tang” (澤漆湯), “Xiao Qing Long Jia Shi Gao Tang” (小青龍加石膏湯), and others.
However, even the disease progressions are similar, the more serious viral attacks like Coronavirus can and often do progress much faster with more severe consequences than the common flu. As described in the previous paragraph, normally the disease progression of the “External Coldness” stage to the more serious “Heated Interior” stage usually takes one to two weeks. It is also often due to the ignorance of the patient or misdiagnosis and treatment of the doctor, which prevents the patient from recovering from this “catching a cold”. In the recent several years though, the time period between the time that the patient did not feel well and the time that the patient is in a serious and complex situation can be as short as 3 to 4 days. We also see the trend that this period gets shorter and shorter. In other words, the disease progression is getting much faster. This phenomenon poses a much higher demand on TCM doctors’ ability to make a quick and proper judgment and sensitivity to the subject matter. TCM doctors must intercept the disease progression before it reaches to a more serious stage, even without obvious symptoms of the next stage. TCM doctors have to timely prescribe the proper herbal remedy in terms of the type of herbs and relative dosages of herbs. Too weak a dosage could not stop the progression while too strong a dosage could worsen the condition also. A misjudgment would not be able the turn the conditions around, but hurt the patient more.
The above can be illustrated by a simple example. A patient caught flu and has symptoms such as heavy coughing, lots of sputum, and difficulty in breathing. From the TCM dialectics, with observations of white tongue coating, clear urine, and feeling chilly, etc., it is clearly caused by “Coldness”. Such a patient typically should be prescribed with “She Gan Ma Hung Tang” (射干麻黃湯) or its variations. However, due to the fast progression of the modern flu, the TCM doctor would need to pay attention to much subtle details such as the dryness of the tongue although it still shows the white coating. In this case, Sheng Shi Gao (Gypsum, 生石膏) might need to be added to the herbal remedy to make sure that the lung would not suffer dryness. Given that Sheng Shi Gao (Gypsum, 生石膏) itself is an ingredicient that is “very cold” in nature, the dosage could not be too strong to make the lung too chilly. At the opposite end of the spectrum, if the sound of the coughing is very ‘deep’, like dense sputum coming from the bottom of the lung, the herbal remedy might need to add Ting Li (Sisymbrium indicum, 葶藶) to clear up the lung to avoid too much mucus in the lung. And again, the dosage of葶藶 could not be too much to weaken the lung. (Note: Handling the proper timing and proper remedy can be a real test to the ability and experience of the TCM doctor.)
Let’s go back to the discussion on Coronavirus. From the limited information available so far, we know that there are about two weeks of incubation period after the infection. There are little symptoms during this period and the patient may just feel more tired than usual. More obvious symptoms will start like those of common flu with fever, fatigue but not too serious. Upper respiratory symptoms like running nose are less common. Some patients may not even exhibit fever. About half of the patients infected will recover over a week or so. The other half of the patients will experience difficulty in breathing, or rapid progression to acute respiratory distress syndrome, septic shock, metabolic acidosis, coagulopathy, etc. Some patients had died due to these severe conditions.
From the above description, this Coronavirus, in the beginning, is very much like the common flu and will stay in stages of “Exterior Deficiency or Weakness” (表虛) or “External Coldness” (表寒). Half of the patients infected will recover by themselves as in common cold. The other half of the patients may exhibit situations of rapid penetration into inner organs and excess ‘heat’, which causes loss of fluidity of respiratory system and accumulation of dense sputum at the lower part of the lung. In the TCM theory, the lung serves as the initial “gating factor” of body fluids. When the lung fails to serve the proper function, other organs like the kidney will be adversely affected also. In other words, Coronavirus can turn a light “External Coldness” to extremely severe “Lung Atrophy” (肺痿) and “Lung Abscess” (肺癰), which in turn will impair the function of other organs.
How to treat? Without direct experience of treating Coronavirus patients, we can only postulate from our limited information available in hand. (Note: Shortly after this writing, the author has directly and indirectly participated in treating patients of Coronavirus successfully. The treatments were exactly as outlined in this article.) From the ample experience of dealing pneumonia cases caused by flu, we are confident that we can also treat Coronavirus successfully. When patients are showing the obvious Coronavirus symptoms, most of them would have entered the stage of “Heated Interior” (入裡化熱) with “Lung Atrophy” (肺痿) or “Lung Abscess” (肺癰) to a certain degree. At this stage, we will need large dosages of Sheng Shi Gao (Gypsum, 生石膏) to clear the heat to ensure the proper fluidity function of the lung. Also, we will rely on Ting Li (Sisymbrium indicum, 葶藶), Da Ji (Euphorbia pekinensis Rupr., 大戟), etc. to clear up the dense mucus at the lower part of the lung and to remove the edema of the chest chamber. Without getting rid of the excess mucus and fluid, the lung cannot properly function. We need to use Ma Huang (Ephedra sinica Stapf., 麻黃), etc. to enhance the lung function (宣肺、發陽) and restore proper breathing. When the lung is damaged as in fibrosis, after the conditions stabilize, we need to “moisturize” the lung (润肺) to help the lung to recover fully. In other words, we will most likely use the herbal ingredients such as Sheng Shi Gao (Gypsum, 生石膏), Ting Li (Sisymbrium indicum, 葶藶), Da Ji (Euphorbia pekinensis Rupr., 大戟), Sheng Ban Xia (Pinellia ternate, 生半夏), Ma Huang (Ephedra sinica Stapf., 麻黃), She Gan (Belamcanda chinensis, 射干), Zi Wan (Aster tataricus, 紫菀), Kuan Dong Hua (Tussilago farfara flower, 款冬花), Sheng Jiang (Ginger, 生薑), Zhi Gan Cao (processed Glycyrrhiza uralensis Fisch., 炙甘草), Hong Zao (Ziziphus jujube, 紅棗), Mai Men Dong (Ophiopogon japonicas, 麥門冬), Xing Ren (Prunus armeniaca, 杏仁), and others. As we discussed in previous paragraphs, the timing, dosage, the relative ratios of different herbal ingredients are very critical. Given that there are quite some variations in patient conditions, the challenges on TCM doctors’ comprehensive knowledge, judgment and courage are unprecedented.
In those articles online, some TCM doctors claimed that Coronavirus can be cured by Ban Lan Gen (Isatis tinctoria root, 板藍根), which is believed to have natural antibiotic chemicals to “clear up the heat and toxics”. Some TCM doctors suggested using a simple mild herbal remedy “Mai Men Dong Tang” (麥門冬湯), which mainly relies on the ingredient Mai Men Dong (Ophiopogon japonicas, 麥門冬). Some people even suggested that having the green bean soup could prevent Coronavirus. In fact, those TCM doctors who have good experience of treating Bird Flu, Swine Flu, and pneumonia caused by other influenza would know that the people making those claims never had the real experience of treating severe pneumonia. They at most helped in a minor way the patients under Western medicine treatments. Under such conditions, it is not a surprise that the China government has not used TCM as the primary method of treating Coronavirus, despite its big promotion of TCM in the recent years. (Note: After this writing, Coronavirus epidemic became so severe in China that the China government changed its strategy and started to use TCM extensively in treating many mild Coronaviurs cases.)
Medicine is the science based on real treatment results. Without a good amount of successful cases in clinical treatments, it is useless to promote any fancy idea of treating patients. If we would like TCM to be respected in the mainstream medicine and to be meaningfully used in a severe epidemic like Coronavirus, it is critical to focus on the most fundamental. That’s the clinical results. Like the martial arts, unless you can fight off the bad guys, it’s just a show of fancy movements.
(http://andylee.pro/wp/?p=7169)
#當張仲景遇上史丹佛
for while 使用 時機 在 コバにゃんチャンネル Youtube 的最佳解答
for while 使用 時機 在 大象中醫 Youtube 的最佳貼文
for while 使用 時機 在 大象中醫 Youtube 的最讚貼文
for while 使用 時機 在 Java學習筆記-迴圈式 的推薦與評價
使用時機. for迴圈多用在已知且低次數的迴圈。 while迴圈多用在未知或高次數的迴圈。 ... <看更多>
for while 使用 時機 在 C++ 教學入門- do while 基本語法與使用時機 - YouTube 的推薦與評價
do while 基本語法與 使用時機 - 022 Statement 9 do while 影片1 : C++ 教學入門 · Comments3. ... <看更多>
for while 使用 時機 在 [問題] 關於fflush的使用時機- 看板C_and_CPP - 批踢踢實業坊 的推薦與評價
使用Lubuntu + gcc 4.9.2
問題(Question):
目前在練習file input/output
有個疑問是如何不要讓前面輸入的Enter影響到後面
看了一下自己手上的書「邊學邊做C語言」是用fflush(stdin)
不過我加進去以後根本沒反應,輸入完要求的char+Enter程式就直接跑到底
然後看了版友的建議用while(getchar()!='\n');
(不過我不太懂,這邊最後一個getchar()不是輸入完要求的char打的'\n'嗎?)
可是的確有用,程式的確停下來叫我輸入string
稍微看了一下好像有些大大說不能用fflush(stdin)
可是google一下發現很多人都在用
我自己對fflush的認識也是把之前輸入到buffer裡的清掉
還是我對fflush的認識有誤?
感謝各位協助。
程式同步分享在此:
https://gist.github.com/gnitnaw/ac3dbcd8fa8e11c515c8
#include <stdio.h>
#define MAXSIZE 256
void read_string(char* p); //可以用scanf或fgets替代,我兩個都不滿意所以自己寫
int main(void) {
char c, s[MAXSIZE];
puts("I/O lib");
puts("");
printf("Please give me a char: ");
c = getchar();
printf("What you keyin is %c\n", c);
fflush(stdin);
while(getchar()!='\n');
printf("Please give me a string : ");
read_string(s);
printf("What you keyin is %s\n", s);
printf("\n Press <Enter> to continue...");
while ((c=getchar()) != '\n');
return 0;
}
void read_string(char* p) {
int i;
char c;
for (i=0; i<MAXSIZE-1; ++i) {
if ( (c=getchar()) != '\n' ) {
p[i] = c;
} else {
break;
}
}
p[i] = '\0';
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 90.41.134.196
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1433943163.A.756.html
... <看更多>