線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1163
推到 Plurk!
推到 Facebook!

將StringGrid裡的資料存成txt檔

尚未結案
superyoung
一般會員


發表:14
回覆:24
積分:7
註冊:2004-07-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-03 00:46:19 IP:61.229.xxx.xxx 未訂閱
請問各位高手 我要將SringGrid裡的資料儲存txt檔 而且要將SringGrid裡的資料以各列的方式存在txt檔裡 即一列一列顯示在txt檔 請問要怎樣寫才能達到我的要求 謝謝
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-03 01:27:07 IP:219.80.xxx.xxx 未訂閱
Hello superyoung,    請參考底下程式碼:
#include     void __fastcall TForm1::Button1Click(TObject *Sender)
{
   StringGrid1->Cells[1][1]="111";
   StringGrid1->Cells[2][2]="222";
   StringGrid1->Cells[3][2]="333";       AnsiString str;
   ofstream outfile("C:\\rowcol.txt");       for (int col=1;col<=StringGrid1->ColCount-1;col  )
       {
         str="";
         for (int row=1;row<=StringGrid1->RowCount-1;row  )
             {
              if (StringGrid1->Cells[row][col]!="")
                  str=str " " StringGrid1->Cells[row][col];
              else
                  str=str "     ";
             }
         outfile << str.c_str() << endl;
       }
   outfile.close();
}
發表人 - nlj859 於 2004/08/03 01:28:48 發表人 - nlj859 於 2004/08/03 15:25:47
superyoung
一般會員


發表:14
回覆:24
積分:7
註冊:2004-07-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-08-03 09:07:47 IP:210.202.xxx.xxx 未訂閱
謝謝nlj859的回覆 可是還是不行耶 還有其他的寫法嗎 謝謝
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-08-03 11:40:32 IP:219.80.xxx.xxx 未訂閱
請問不行是怎麼不行法? 可以詳細說明你要的是什麼結果嗎?
superyoung
一般會員


發表:14
回覆:24
積分:7
註冊:2004-07-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-08-03 12:09:13 IP:210.202.xxx.xxx 未訂閱
我用nlj859您提供的程式 可是結果並非將StringGrid裡各列的資料 以各列的方式顯示在txt檔 ex:在stringGrid裡的資料 第一列 12 55 66 77 第二列 58 22 55 66 那在txt檔裡也是以同樣的方式顯示出來 第一列 12 55 66 77 第二列 58 22 55 66 麻煩nlj859您了...謝謝
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-08-03 12:52:42 IP:219.80.xxx.xxx 未訂閱
Hello superyoung,    不曉得你有沒有看存在c:\rowcol.txt那個文字檔? 程式真的照你的意思一樣,StringGrid和.txt的內容都一樣.
#include 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   StringGrid1->Cells[1][1]="12";
   StringGrid1->Cells[2][1]="55";
   StringGrid1->Cells[3][1]="66";
   StringGrid1->Cells[4][1]="77";
   StringGrid1->Cells[1][2]="58";
   StringGrid1->Cells[2][2]="22";
   StringGrid1->Cells[3][2]="55";
   StringGrid1->Cells[4][2]="66";       AnsiString str;
   ofstream outfile("C:\\rowcol.txt");       for (int col=1;col<=StringGrid1->ColCount-1;col  )
       {
         str="";
         for (int row=1;row<=StringGrid1->RowCount-1;row  )
             {
              if (StringGrid1->Cells[row][col]!="")
                  str=str " " StringGrid1->Cells[row][col];
              else
                  str=str "   ";
             }
         outfile << str.c_str() << endl;
       }
   outfile.close();
}
發表人 - nlj859 於 2004/08/03 15:25:13
superyoung
一般會員


發表:14
回覆:24
積分:7
註冊:2004-07-02

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-08-03 13:21:29 IP:210.202.xxx.xxx 未訂閱
謝謝nlj859的解答.... 如果希望原本在StringGrid裡的各行資料 至txt檔時各行的資料都能對齊 請問要怎樣寫呢??? 再次麻煩nlj859您了....謝謝
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-08-03 15:15:55 IP:163.28.xxx.xxx 未訂閱
Hello superyoung,    假設你每個cell裡面的字最多10個,那麼程式更改如下:
   for (int col=1;col<=StringGrid1->ColCount-1;col  )
       {
         str="";
         for (int row=1;row<=StringGrid1->RowCount-1;row  )
             {
              int CellsLength=StringGrid1->Cells[row][col].Length();
              if (StringGrid1->Cells[row][col]!="")
                  str=str StringGrid1->Cells[row][col] AnsiString::StringOfChar(' ',10-CellsLength) " ";
              else
                  str=str AnsiString::StringOfChar(' ',10) " ";
             }
         outfile << str.c_str() << endl;
       }
發表人 - nlj859 於 2004/08/03 15:17:58 發表人 - nlj859 於 2004/08/03 15:24:19
superyoung
一般會員


發表:14
回覆:24
積分:7
註冊:2004-07-02

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-08-03 16:06:20 IP:210.202.xxx.xxx 未訂閱
非常感謝nlj859 讓我學到不少的東西 而且也解決了我的問題 
系統時間:2024-09-30 13:04:06
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!