Author Topic: Reading a Text file  (Read 9501 times)

fasu

  • Newbie
  • *
  • Posts: 6
Re: Reading a Text file
« Reply #30 on: June 03, 2022, 11:13:05 AM »
Thank you for your reply.

Quote
How did you perform this "conversion"? A CSV file is literally a text file with specific formatting and a different extension.
==> Open the .csv file with Windows 10 Bloc-Note, then SAVE AS .txt extention.

Thanks for the advice on how to design the code. I will try on these bases.

fasu

  • Newbie
  • *
  • Posts: 6
Re: Reading a Text file
« Reply #31 on: June 03, 2022, 12:53:14 PM »
Yes! Nailed it !
I'm pretty proud because I'm starting from scratch in C#.
Here is my code (with a lot of yours anyway ;-) )
Code: [Select]
using System;
using System.Text;
using System.Windows.Forms;

public class VAInline
{
public void main()
{
string input = VA.GetText("~textFile");

if (input == null)
{
VA.WriteToLog("Error: \"~textFile\" has not been set", "red");
return;
}
string target = VA.GetText("~target");
if (input == null)
{
VA.WriteToLog("Error: \"~target\" has not been set", "red");
return;
}
string[] lines = input.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
for (int targetLine = 0 ; targetLine <= lines.Length  ; targetLine++)
{
//VA.WriteToLog("Loop stage : " + targetLine , "orange");
string targetRow = lines[targetLine];
bool test = targetRow.Contains(target);
//VA.WriteToLog("test result : " + test , "blue");
if (test == true)
{
//VA.WriteToLog("The searched row number (airportID) = " + targetLine , "black");
VA.SetInt("airportID", targetLine );
return;
}
}
}
}

I have a little time to understand my mistakes, but finally it works perfectly.
Thank you very much for all the scripts and comments posted here @Pfeil, because it was thanks to them that I was able to achieve this.


fasu

  • Newbie
  • *
  • Posts: 6
Re: Reading a Text file
« Reply #32 on: June 04, 2022, 12:00:47 AM »
Hello,
To help, if anyone is looking, here is the same code to look up the (0-based) number of a column :

Code: [Select]
using System;
using System.Text;
using System.Windows.Forms;

public class VAInline
{
public void main()
{
string input = VA.GetText("~textFile");

if (input == null)
{
VA.WriteToLog("Error: \"~textFile\" has not been set", "red");
return;
}
string target = VA.GetText("~target");
if (input == null)
{
VA.WriteToLog("Error: \"~target\" has not been set", "red");
return;
}
string[] lines = input.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
string[] sections = lines[0].Split(',');

for (int targetSection = 0 ; targetSection <= sections.Length  ; targetSection++)
{
//VA.WriteToLog("Loop stage : " + targetSection , "orange");
string targetColumn = sections[targetSection];
bool test = targetColumn.Contains(target);
//VA.WriteToLog("test result : " + test , "blue");
if (test == true)
{
VA.WriteToLog("The searched column number (columnID) = " + targetSection , "black");
VA.SetInt("columnID", targetSection );
return;
}
}
}
}