Read .txt file, send Line and Delete it after acknowledgment of receipt

2

This code is the fragment of a method that reads a text file and sends its contents line by line, where each sent line receives a receipt acknowledgment. I would like to erase the sent line from the file so that it receives its acknowledgment of receipt, so that if the transmission is interrupted in the middle of the file, I do not send the lines that have already been sent, thus avoiding to duplicate the information received by the equipment. Could you please help me with this?

 //Path ex : "/home/dhnqe/DataContainer.txt"
  if (File.Exists(path))
  {
    StreamReader file = new StreamReader(path);
    int numeroLinhas = File.ReadAllLines(path).Length;
    while ((linha = file.ReadLine()) != null)
    {
        linha = string.Format(linha + "\r\n");
        for (int i = 0; i< 3; i++)
        {
        controle = false;
        SendLineByRadio(address, line); //Enviar Linha
        int start = Environment.TickCount;
        do
        {
          ...
          ...
        } while (!controle && !timeout);

        if (controle)
        {
            if (Confirmation == "ReceiveOK")//Caso receba a confirmação
            {
                Console.WriteLine("Data Sent  = OK");
                clearData();
                controle = false;
                //INSERIR FUNCAO PARA APAGAR LINHA ENVIADA DO ARQUIVO TXT
                i = 10;
            }
        }
        if (timeout)
        {
            Console.WriteLine("Data Sent = Confirmation Timeout");//Caso não recebe a confirmacao
            clearData();
            controle = true;
            if (i == 2)
            {
                Console.WriteLine("Error: Response not received");
                clearData();
                Thread.CurrentThread.Abort();
            }

        }
     }
   }
    
asked by anonymous 09.08.2016 / 15:19

1 answer

1

Well, the answer was to create the variable lineCounter to count the lines sent successfully.

   //Path ex : "/home/dhnqe/DataContainer.txt"
    if (File.Exists(path))
    {
      int lineCount = 0; // VARIAVEL CRIADA PARA CONTAR AS LINHAS ENVIADAS
      StreamReader file = new StreamReader(path);
      int numeroLinhas = File.ReadAllLines(path).Length;
      while ((linha = file.ReadLine()) != null)
      {
          linha = string.Format(linha + "\r\n");
          for (int i = 0; i< 3; i++)
          {
          controle = false;
          SendLineByRadio(address, line); //Enviar Linha
          int start = Environment.TickCount;
          do
          {
            ...
            ...
          } while (!controle && !timeout);

          if (controle)
          {
              if (Confirmation == "ReceiveOK")//Caso receba a confirmação
              {
                  lineCount++; //INCREMENTAR, POIS A LINHA FOI ENVIADA E A RESPOSTA FOI RECEBIDA
                  Console.WriteLine("Data Sent  = OK");
                  clearData();
                  controle = false;
                 i = 10;
              }
          }
          if (timeout)
          {
              Console.WriteLine("Data Sent = Confirmation Timeout");//Caso não recebe a confirmacao
              clearData();
              controle = true;
              if (i == 2)
              {
                  Console.WriteLine("Error: Response not received");
                  clearData();
                  Thread.CurrentThread.Abort();
              }

          }
       }
     }

From Variable the function ApagarLinhas() Le the file and copy the lines that were not created to a temporary file, then replace the file with the original, then erase the backup file.

  private void ApagarLinhas(string fileInput,int line_to_keep)
      {
        string line = null;
        int line_number = 0;
        string tempFile = fileInput + ".tmp";
        string backupFile = fileInput+"backup";
        using (StreamReader reader = new StreamReader(fileInput))
        {
            using (StreamWriter writer = new StreamWriter(tempFile))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    line_number++;

                    if (line_number <= line_to_keep)
                        continue;

                    writer.WriteLine(line);
                }
            }
        }
        File.Replace(tempFile, fileInput, backupFile);
        File.Delete(backupFile);                 

      }
    
09.08.2016 / 20:11