0

I am starting to move from VB6 to C#. Although I am a experienced programmer (40+ Years) often it is knowing the newer constructs that are the big help rather than diligently translating line by line. Out company receives orders by text file which consist of a list of lines.

XXX=param1;param2
YYY=param1;param2

In VB6 I have created a very complex string class for searching, extracting, updating, adding, deleting, and inserting these lines.

Does C# offer an already existing construct or will I need to write my own. The Dictionary construct looks good but I am concerned re the number of values, as well as if there is a way that I can import the file in one go without having to read it line by line.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • File.ReadLines(). Then split each line on the ‘;’ character – maccettura Jun 24 '18 at 03:41
  • @maccettura I removed my comment – Anirudha Gupta Jun 24 '18 at 03:50
  • 1
    @Ajay2707 read my mind about using a List. With a list you have built-in methods to perform further actions on the content as well. – nocturns2 Jun 24 '18 at 04:04
  • 1
    How big is you input text file? Very large files you probably want to read line by line so you do not use lots of memory. I've been programming for 40 years and nothing has really changed in parsing text files. Some people use Regex put it is as efficient and doesn't handle inputs that have lots of different formats. I sometimes use Regex to parse one line into separate parameters. – jdweng Jun 24 '18 at 07:57

2 Answers2

1

No, C# doesn't offer this kind of things, you'll need to write your own file parser (probably like the one you already have in VB6).

I don't think the Dictionary will help you here (unless I didn't understand your file structure), dictionaries are for Key-Value pairs (so it's 2 values per entry) but you seem to have plain params (meaning that your entries don't look like key value pairs).

It would be much easier for you to just create 1-2 classes to do the parsing using mainly:

  • File.ReadAllLines to read the text files and get an array of lines.
  • line.Split(';') to get the parameters of each line (assuming you foreach the lines).
Haytam
  • 4,643
  • 2
  • 20
  • 43
1

You are not mention the C# version, @jonskeet mention in this answer about File.ReadLines,

If you're using .NET 4, simply use File.ReadLines which does it all for you. I suspect it's much the same as yours, except it may also use FileOptions.SequentialScan and a larger buffer (128 seems very small).

Based on the above answer I search that there is option to convert file result to list and I got the answer, so after converting you use linq too.

List<string> allLinesText = File.ReadAllLines(fileName).ToList()
maccettura
  • 10,514
  • 3
  • 28
  • 35
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
  • Why use ReadLines() and then immediately call ToList()? Either perform some processing on the resulting IEnumerable from ReadLines() or just call File.ReadAllLines(). There is a difference (IEnumerable Vs List) – maccettura Jun 24 '18 at 04:11
  • .ToList will convert to array result to list, see in the @Rama result in second links. – Ajay2707 Jun 24 '18 at 04:20
  • I was mistaken. ReadAllLines() returns an Array. Its still weird to not use ReadAllLines() if you’re not doing any processing on the IEnumerable though. – maccettura Jun 24 '18 at 04:26