กองทุน SPDR GOLD SHARES
ถือทองก่อนหน้า
ถือทองล่าสุด
0.00
*หน่วยตัน / ราคาอ้างอิงล่าสุดจากสมาคมค้าทองคำ
สถิติกองทุน SPDR
ราคาทองคำแท่ง 96.5%
ราคาอ้างอิงล่าสุดจากสมาคมค้าทองคำ
ครั้งที่
ราคาก่อนหน้า
ราคาล่าสุด
0
(หน่วย บาท*) / อัปเดตล่าสุดเมื่อวันที่ 13 ก.ค. 2566 เวลา 13:04 น.
สถิติราคาทองคำ ไทย

การดึงค่าจากไฟล์ csv

  • 4 replies
  • 1,632 views
การดึงค่าจากไฟล์ csv
« เมื่อ: 22, สิงหาคม 2022, 11:07:11 AM »
//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//-------------------------------------------------------------------
int         filehandle;
int         str_size;
int         TP_Buy, SL_Buy;
int         TP_Sell, SL_Sell;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL5\\Files\\"+"csv.csv";
   //filehandle=FileOpen(filename,FILE_READ|FILE_CSV);
   ResetLastError();
   filehandle=FileOpen("csv.csv",FILE_READ|FILE_CSV);
   //--------------------------
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }
 
//--- correct way of working in the "file sandbox"
   if(filehandle!=INVALID_HANDLE)
      {
            while(!FileIsEnding(filehandle))
              {
                  TP_Buy = (int)StringToInteger(FileReadString(filehandle));
                  SL_Buy = (int)StringToInteger(FileReadString(filehandle));
                  TP_Sell = (int)StringToInteger(FileReadString(filehandle));
                  SL_Sell = (int)StringToInteger(FileReadString(filehandle));
                  Print("TP : ",TP_Buy," sl : ",SL_Buy);
              }
            FileClose(filehandle);
      }
//--------------------------
//Comment(filehandle," tp b : ",TP_Buy," sl b : ",SL_Buy);
  }
//+------------------------------------------------------------------+


ช่วยดูโค๊ดให้หน่อยครับ ผมลองดึงค่า ซึ่งมันมีไฟล์ csv.csv อยู่แล้ว แต่ดึงค่าออกมาอย่างไรก็ได้ 0 อยู่ดีครับ

*

iRuler

  • 1,282
Re: การดึงค่าจากไฟล์ csv
« ตอบกลับ #1 เมื่อ: 22, สิงหาคม 2022, 12:29:39 PM »
โค๊ด [Select]
while(!FileIsEnding(filehandle))
      {
         /*
            TP_Buy = (int)StringToInteger(FileReadString(filehandle));
            SL_Buy = (int)StringToInteger(FileReadString(filehandle));
            TP_Sell = (int)StringToInteger(FileReadString(filehandle));
            SL_Sell = (int)StringToInteger(FileReadString(filehandle));
            */
         string read_line =  FileReadString(filehandle);
         //Print("TP : ", TP_Buy, " sl : ", SL_Buy);

         string array[];
         StringSplit(read_line, ',', array);
         Print("TP : ", array[0], " sl : ", array[1]);
      }


(TH)**

FileReadString() มันจะอ่านมาทั้งบันทัดครับ
บันทัดแรกของไฟล์ csv ไม่ต้องใส่ครับ หรือจะใส่ก็ให้ skip บันทัดแรกตอนวนลูปอ่าน

Re: การดึงค่าจากไฟล์ csv
« ตอบกลับ #2 เมื่อ: 22, สิงหาคม 2022, 01:58:58 PM »
ผมลอง ทดสอบดูค่าที่ได้มันเป็นภาษาจีนครับ ไม่เหมือนค่าในไฟล์ csv เลยครับ

*

iRuler

  • 1,282
Re: การดึงค่าจากไฟล์ csv
« ตอบกลับ #3 เมื่อ: 23, สิงหาคม 2022, 09:06:25 AM »
น่าจะเป็นเพราะไฟล์ csv encoding เป็น UTF-8 ครับ ลอง convert เป็น ANSI ครับ



Re: การดึงค่าจากไฟล์ csv
« ตอบกลับ #4 เมื่อ: 11, กันยายน 2022, 09:36:11 PM »
 Ha)**