CSV Read/Write and remove a line row

 

Hello,

i was reading that article https://www.mql5.com/en/articles/2720 i have got most of what i need i learned how to write and read csv lines.

but i couldnt fix remove a csv line row or in other term file line content.

here is a test.csv

Line-1-1;Line-1-2;Line-1-3
Line-2-1;Line-2-2;Line-2-3
Line-3-1;Line-3-2;Line-3-3
Line-4-1;Line-4-2;Line-4-3
Line-5-1;Line-5-2;Line-5-3

when i wanted to remove the the 3rd line so i thought it will get sorted so the 4th will be the 3rd and so on, here is the script i used the idea

#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
   
   // open the file to be changed
   int h=FileOpen("test.csv",FILE_READ|FILE_ANSI|FILE_CSV);
   
   if(h==INVALID_HANDLE){
      Alert("Error opening file");
      return;
   }
   
   // receive the name of the temporary file
   string tmpName=TmpFileName("test","csv");
   
   //open the temporary file
   int tmph=FileOpen(tmpName,FILE_WRITE|FILE_ANSI|FILE_CSV);
   
   if(tmph==INVALID_HANDLE){
      Alert("Error opening temporary file");
      return;
   }   
  
   // read the file line by line and count the number of lines
   int cnt=0;
   while(!FileIsEnding(h)){
      cnt++;
      string str=FileReadString(h);
      if(cnt==3){
         // replace the line
         FileWrite(tmph,"");
      }
      else{
         // rewrite the line with no changes
         FileWrite(tmph,str);
      }
   }
   
   // close the files
   FileClose(tmph);
   FileClose(h);
   
   // delete the source file, rename the new one
   //FileDelete("test.txt");
   FileMove(tmpName,0,"test.csv",FILE_REWRITE);
   
   //FileCopy(tmpName,0,"test.txt",FILE_REWRITE);
   //FileDelete(tmpName); 

   Print("Done");
}
//+------------------------------------------------------------------+

string TmpFileName(string Name,string Ext){
   string fn=Name+"."+Ext; // forming name
   int n=0;
   while(FileIsExist(fn)){ // if the file exists
      n++;
      fn=Name+IntegerToString(n)+"."+Ext; // add a number to the name
   }
   return(fn);
}


here where the 3rd line to get modified

      if(cnt==3){
         // replace the line
         FileWrite(tmph,"");
      }

after the above will the result

Line-1-1;Line-1-2;Line-1-3
Line-2-1;Line-2-2;Line-2-3

Line-4-1;Line-4-2;Line-4-3
Line-5-1;Line-5-2;Line-5-3

how can i sort the 4 and 5th line to move up.., my project scripts write and read like afew lines and somehow i do remove random csv line rows, but if i didnt fix it so will be new line and lines get empty tables and a file of full empty lines

MQL5 Programming Basics: Files
MQL5 Programming Basics: Files
  • www.mql5.com
Like many other programming languages, MQL5 features the functions for working with files. Although working with files is not a very common task when developing MQL5 Expert Advisors and indicators, each developer faces it sooner or later. The range of issues that require working with files is wide enough. It includes generating a custom trading...
 
mohdqallaf: but i couldnt fix remove a csv line row or in other term file line content.

Because you can't. They are variable length lines. Read them in to an array, seek to the beginning, write back out what you want.

 
William Roeder:

Because you can't. They are variable length lines. Read them in to an array, seek to the beginning, write back out what you want.

Could you give me a sulotion that read and remove

And another write..?
 
mohdqallaf:
Could you give me a sulotion that read and remove

And another write..?
Maybe you might consider using sqlite3 databases. I took a day and mastered everything. It's much better than working with files.
Reason: