1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
   | bool __fastcall TFormHoroBourse::MyDownloadToFile(String URLSourceFile,String DestFile)
{
  if(FileExists(DestFile)) DeleteFile(DestFile);
 
  char cBuffer[1024];
  FILE *fichier;
  DWORD dwBytesRead;
 
  String AppName = ExtractFileName(Application->ExeName);;
  HINTERNET hInternetSession =InternetOpen(AppName.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  try{
              HINTERNET hURL = InternetOpenUrl(hInternetSession, URLSourceFile.c_str(), NULL,0,0,0);
              try
              {
                        fichier= fopen(DestFile.c_str(),"ab+");
 
                        do
                        {
                                InternetReadFile(hURL,(LPSTR)cBuffer,(DWORD)1024,&dwBytesRead);
                                fwrite(cBuffer, dwBytesRead, 1, fichier);
                        }
                        while (dwBytesRead!=0);
 
                        fclose(fichier);
                        delete fichier;
                        InternetCloseHandle(hURL);
                        InternetCloseHandle(hInternetSession);
                        return true;
                }
                catch(...)
                {
                        InternetCloseHandle(hURL);
                        InternetCloseHandle(hInternetSession);
                        return false;
                }
              }
              catch(Exception &e)
              {
                        InternetCloseHandle(hInternetSession);
                        return false;
              }
}
void __fastcall TFormHoroBourse::TimerBourseTimer(TObject *Sender)
{
 
 
      String DestFile = ExtractFilePath(Application->ExeName)+FormatDateTime("dd-mm-yy_hhmmssms",Now())+".txt";;
      String Url = "http://www.euronext.com/tools/datacentre/dataCentreDownloadExcell.jcsv?cha=3042&lan=FR&idInstrument=16916&isinCode=FR0000125007&indexCompo="eon=&time=on&volume=&typeDownload=1/";
 
      if(! MyDownloadToFile(Url,DestFile))     // téléchargement
      {
        LabelBourse->Caption="Cours de l'action indisponible";
        //ShowMessage("fichier non téléchargé");
      }else {
          TStringList * log =  new TStringList();
          log->LoadFromFile(DestFile);          // mise en mémoire de toute les lignes
 
          if(FileExists(DestFile)) DeleteFile(DestFile);
 
 
          // pour ton cas avec nom et id , on verifie qu'il y a bien un nombre pair de ligne
          if(log->Count%2!=0) {
                LabelBourse->Caption="Anomalie dans le fichier";
                return;
          }
          // Récupération des nom et id dans 2 listes différentes
          TStringList * list_nom = new TStringList();
          TStringList * list_id = new TStringList();
 
          for(int i=0;i<log->Count;i++){
             if(i%2==0){                                //ligne contenant le nom
                 list_nom->Add(log->Strings[i]);
             }else{
                 list_id->Add(log->Strings[i]);       //ligne contenant l'id
             }
          }
          //affichage dans un memo des noms puis des id........
 
 
          TSysCharSet     Separators,WhiteSpace;
          Separators<<'\t';
          ExtractStrings(Separators,WhiteSpace,list_id->Strings[1].c_str(),list_nom);
 
          LabelBourse->Caption=" Cours de l'Action SAINT GOBAIN : "+list_nom->Strings[9]+"  \n MàJ : "+list_nom->Strings[12]+" ";
 
          //pms->Clear();
          delete list_nom;
          delete list_id;
 
      }
 
 
} | 
Partager