今天又突發奇想,想列印一個字符畫出來,花了半天純手工把70行字符定義成了字串,并串成了一個陣列,
結果這玩意兒里有一堆字符編譯器不認,要是調整漸變字符再弄就又要花半天,實在受不了那氣,決定弄一個能自動讀取txt中字符保存到字串的程式,
20多行代碼,共花費了兩個小時,其中45分鐘上網上找代碼,45分鐘理解代碼,30分鐘重寫代碼,
//選擇要讀取哪一行,傳遞檔案路徑
string GetCertainRowFile(const int CertainLine,const string FilePath)
{
const char *temp = FilePath.c_str();
FILE *fp = fopen(temp,"r");
if(fp==NULL)
{
const string ErrorReport = "You opened up the emptiness";
cout<<ErrorReport<<endl;//報錯
return ErrorReport;
}
int currentLine = 1;//當前讀取的行
const int MAX = 1024;
while(!feof(fp))
{
char str[MAX];
fgets(str,MAX,fp);
if(CertainLine==currentLine)
{
return str;
}
currentLine++;
}
fclose(fp);//關閉檔案
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260151.html
標籤:其他
上一篇:利用卷積層實作滑動視窗(Convolutional implementation of sliding windows)
下一篇:由淺入深了解JVM
