在BCB利用ofstream輸出資料到文字檔的詭異結果? |
答題得分者是:daldal
|
sunwingman
一般會員 發表:26 回覆:47 積分:14 註冊:2008-05-14 發送簡訊給我 |
以下我的程式碼片段:
[code cpp] void __fastcall TForm1::GetRGB1Click(TObject *Sender) { int r,g,b; for(int j=startY; j<=endY; j ) for(int i=startX; i<=endX; i ) { r = GetRValue(bmp->Canvas->Pixels[i][j]); g = GetGValue(bmp->Canvas->Pixels[i][j]); b = GetBValue(bmp->Canvas->Pixels[i][j]); } r = r/((endX-startX 1)*(endY-startY 1)); //將一個區塊裡面的R值做平均 g = g/((endX-startX 1)*(endY-startY 1)); //將一個區塊裡面的B值做平均 b = b/((endX-startX 1)*(endY-startY 1)); //將一個區塊裡面的G值做平均 ofstream outfile("C:\\rgb.txt",ios_base::app); ofstream cerr; if(!outfile) cerr << "some problem happen to file!"; else outfile << r << ' ' << g << ' ' << b << endl; outfile.close(); } [/code] 在文字檔裡面的卻會產生r,g,b未做平均的值! 可以請教一下各位為什麼會這樣?謝謝~ |
daldal
高階會員 發表:6 回覆:102 積分:226 註冊:2007-06-18 發送簡訊給我 |
放一個Image1元件, 元件載入一張圖片
執行下面Code 我輸出的結果是有平均過的113 113 110 用Debug去看沒平均的值也正常喔 38210711, 38350407, 37310148 圖片的長寬這裡是704x480 [code cpp] int r,g,b; for(int j=0; j for(int i=0; i { r = GetRValue(Image1->Canvas->Pixels[i][j]); g = GetGValue(Image1->Canvas->Pixels[i][j]); b = GetBValue(Image1->Canvas->Pixels[i][j]); } r = r/(Image1->Width*Image1->Height); //將一個區塊裡面的R值做平均 g = g/(Image1->Width*Image1->Height); //將一個區塊裡面的B值做平均 b = b/(Image1->Width*Image1->Height); //將一個區塊裡面的G值做平均 ofstream outfile("C:\\rgb.txt",ios_base::app); ofstream cerr; if(!outfile) cerr << "some problem happen to file!"; else outfile << r << ' ' << g << ' ' << b << endl; outfile.close(); [/code] |
sunwingman
一般會員 發表:26 回覆:47 積分:14 註冊:2008-05-14 發送簡訊給我 |
難道是我的電腦有問題?
因為如果我把輸出檔案的部份另外寫成一個副程式 然後再用呼叫的方式 那麼就不會有我說得那個問題存在,如下: [code cpp] void __fastcall TForm1::GetRGB1Click(TObject *Sender) { for(int j=startY; j<=endY; j ) for(int i=startX; i<=endX; i ) { R = GetRValue(bmp->Canvas->Pixels[i][j]); G = GetGValue(bmp->Canvas->Pixels[i][j]); B = GetBValue(bmp->Canvas->Pixels[i][j]); } R = R/((endX-startX 1)*(endY-startY 1)); G = G/((endX-startX 1)*(endY-startY 1)); B = B/((endX-startX 1)*(endY-startY 1)); ShowMessage("Done!"); ExportData(); //呼叫 } void __fastcall TForm1::ExportData() { //TODO: Add your source code here AnsiString str = ExtractFilePath(ParamStr(0)) "\\rgb.txt"; char *dir = str.c_str(); ofstream outfile(dir,ios_base::app); ofstream cerr; if(!outfile) cerr << "some problem happen to file!"; else outfile << R << ' ' << G << ' ' << B << endl; outfile.close(); } [/code] |
daldal
高階會員 發表:6 回覆:102 積分:226 註冊:2007-06-18 發送簡訊給我 |
|
sunwingman
一般會員 發表:26 回覆:47 積分:14 註冊:2008-05-14 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |