Problem to read a csv file.

 

I want read dates of a csv file using Filereadarray.

 

The problem is that the program read a values different of that csv file. 

The file is a csv file with one column (of Excel) with the values 1 2 3 4 5 6 7 8 9. The program read a different value.

 

I cant use Fileopen with the flag FILE_CSV because I don't read the file (return value -1). If  I use the flag FILE_BIN open the file, but read a different value.

 I have read many articles but I havent' found nothing about this case.

 

I attach the code.

 

 /

/+------------------------------------------------------------------+

//|                                                     alberto1.mq4 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2015, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict


input string  InpFileName="campo.csv";

int file_handle;

string filename1;

string filename2;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//---


//---

   return(INIT_SUCCEEDED);

  }


//+------------------------------------------------------------------+

//| Expert start function                                   |

//+------------------------------------------------------------------+

int start()

  {

int  arr[];

//--- read all data from the file to the array

file_handle= FileOpen(InpFileName,FILE_READ|FILE_BIN);

   FileReadArray(file_handle,arr);

//--- receive the array size

    int size=ArraySize(arr);

//--- print data from the array

   for(int i=0;i<size;i++)

      Alert("Date = ",arr[i]);

 //--- close the file

 FileClose(file_handle);

The file a excel file with one column with values 1 2 3 4 5 6 7 8 9

 

Best regards.

 

Alberto 

 

A csv-file is a file of strings organized in rows - delimiter;"\n" and columns delimiter:";" (?)!

1) I would first get the ulong filesize=FileSize(..);

2) Now I would read the whole file into s a string sAll=FileReadString(..);

3) Now you have to split sAll into rows "\n" and than each row ";" into columns and now you have to convert each column-string into a value like FileReadInteger(..), or FileReadFloat(), or ...


 
ajarenillasp:

I want read dates of a csv file using Filereadarray.

The file is a csv file with one column (of Excel) with the values 1 2 3 4 5 6 7 8 9. The program read a different value.

The file a excel file with one column with values 1 2 3 4 5 6 7 8 9
Make up your mind; it's either a csv or it's a binay excel file that looks like this:
PK    ›”G…l9Š.   .      mimetypeapplication/vnd.oasis.opendocument.spreadsheetPK    ›”G               Configurations2/floater/PK  ›”G            '   Configurations2/accelerator/current.xml PK           PK    ›”G               Configurations2/images/Bitmaps/PK    ›”G               Configurations2/progressbar/PK    ›”G               Configurations2/menubar/PK    ›”G               Configurations2/popupmenu/PK    ›”G               Configurations2/statusbar/PK    ›”G               Configurations2/toolbar/PK    ›”G               Configurations2/toolpanel/PK    ›”G:‘io"  o"     Thumbnails/thumbnail.png‰PNG

  
IHDR   Í      Ÿßeà  "6IDATxœí]Í®âÆÖÝHy„¤ó6Rç@É•23L¬VÄ”™‰”L˜!µZbÆW
̘¢«ˆ    ‡Ù•¾{/pZ¼@’~¾úñOU¹ æ0ötÿ—Ë›r¹V­µ¿:€@\_ݺ ˆ§€g«v©>
þ®ŽüuÇ íÇ5³»p_û¼¬Úµ]/¨ÄùâŒE“õr8ØÑb
h¨­†ó¦X$Û«'Ž4{²~Ú{¿xœñhj,k¥úª®kUf~³5\uz;h6èØvÜÁnöóü Ãæ7í¼=+ko³±ž»m
Xœ­^³ãVÔt-Ê,o·÷·4n]Æ[`? oÞI„÷ër°~cõزgûg•a9°úkÞ\U+&ì—d¥a‚µ»mo»™vKAwÕu Œ3Z5ˆó‘øÞ$?d¯9#ý²®å€ÄÝ|¹ï÷Æbj9“[”ð60+U÷e¿W‹[–¦ø qf4šÞПô¡T*ÑÞLÍí•ÐJ¶{́Yê²ïÍgz]^´.Ê'öGkÏx¥.H„·X“    ýâ,Õš´ÜY:7-â`OÂê Xë°oö:ïMR©>‰¬R=\O~Çõó|\ýôôF>VÇë÷Ïž¶á:

Did you save as and set the file type? You cant read excel files.
 
WHRoeder:
Make up your mind; it's either a csv or it's a binay excel file that looks like this:Did you save as and set the file type? You cant read excel files.

 Many thanks

The file that I want read is a csv file. This file was created by Excel and saved as csv (option of Excel).

If I open this file with any software of text (example notepad) I don't see any symbol (/n, ;). I see with an aspect very similar to excel.

 

If I put in the code 

file_handle= FileOpen(InpFileName,FILE_READ|FILE_CSV);

I obtain an handle -1

But if I write  

 

file_handle= FileOpen(InpFileName,FILE_READ|FILE_CSV); 

I obtain a handle 1. In this case, I obtain values very different values that the files 

 

I attach the file  

Files:
campo.zip  1 kb
 
ajarenillasp: If I put in the code 
file_handle= FileOpen(InpFileName,FILE_READ|FILE_CSV);

I obtain an handle -1

What is the error? find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 

Many thanks for your help, but I don't find  the solution.

 

I want resume all

 

When I compile I obtain 0 errors and 0 warnings.

When I run the code, I have put a Watch to some parameters to array (size and the array()).

Before, I said that when I put   

file_handle= FileOpen(InpFileName,FILE_READ|FILE_CSV);

I obtain a value ofr -1. It's false, it's an error mine. 

When I put this code, I obtain a size of array of 0, and don't read any value.

 

 Instance when I put the code 

 file_handle= FileOpen(InpFileName,FILE_READ|FILE_BIN);

The same file I obtain a size different of zero and read values (differents to the file)

 

I attach the code and excel for understand better.

 

I am sure that it's a mine error, but I don't know  

Files:
files.zip  9 kb
Reason: