2

I have a text file which is as follows:

Ali 

M*59*AB

John

M*68*B

Shirley

F*35*B

Peter

M*88*A

Fiona

F*55*O

Mary

F*46*B

How do I effectively read two lines of data from a text file and assign into variables where 1st line is name, and 2nd line is GENDERWEIGHTBLOODTYPE?

2 Answers2

1

You can try the following steps:

  1. Remove/Filter empty lines from file
  2. Read 2 lines at a time
  3. Split the 2nd line with * and read the value in array

Since you haven't provided any code I won't write them for you, instead I am assuming a json file as output. Note: Here the SO.txt contains your example data.

var f = File.ReadAllLines(@"C:\Temp\SO.txt")
    .Where(_=>_.Length > 0).ToArray() ;//(1) Filter empty lines

for(int i = 0; i< f.Length; i+=2) //Read 2 lines at time
{
    var firstLine = f[i]; //firstline
    var secondLine = f[i+1].Split('*'); //secondline
    var Gender = secondLine[0]; //1st element of second line after split
    var Weight = secondLine[1]; //2nd element of second line after split
    var BloodType = secondLine[2]; //3rd element of second line after split

    var line = $"{{\"Name\":\"{firstLine}\", \"Gender\":\"{Gender}\", \"Weight\": {Weight}, \"BloodType\": \"{BloodType}\"}}";
    
    Console.WriteLine(line);
}   

The output will be something like this:

enter image description here

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
  • Except that you didn't "name" your variables perfectly and didn't really focus on his question. Is that actually the best answer, unless the file is huge, then "File.ReadAllLines" is always a bad choice. – Schecher_1 Mar 22 '23 at 11:22
  • @Schecher_1 Even I am failing to understand your comment, you have issue with variable name or my focus ? and this is for those 23 lines in the example and file size is not mentioned anywhere. Moreover, this is an example code not a software project, so it won't be as per everyones expectations. And rather than telling give a good choice and explain why to be more constructive. – Vinod Srivastav Mar 22 '23 at 20:36
  • He wrote "How do I effectively read..." this means then also e.g. if it is a larger file to use a stream. I did not want to attack you but cmon we both know that "f" is a very very bad var name, what should someone imagine under f? better would be e.g. "fileValue" "allFileLines" "allLines" but not f. In addition what I noticed, you wrote e.g. "firstLine" in camelCase and "BloodType" in PascalCase. However, your answer is correct! Translated with www.DeepL.com/Translator (free version) – Schecher_1 Mar 22 '23 at 21:19
  • C# is one of the things while I also write `python` & 'JavaScript` and that's where `file f` came from. And believe me if you view source of google.com the first variable is `f` but if you say so as `int i` & `` are also and don't tell me you never used them. But if someone don't understand what `File.ReadAllLines` does he will never understand this example even with the comment. – Vinod Srivastav Mar 22 '23 at 21:44
  • But you mentioned the file content 'f', if that was a filesstream, it would be something completely different. I mean that it's a bad idea with 'File.ReadAllLines' because it loads the whole file in RAM. And with large files, it is very unfortunate. – Schecher_1 Mar 23 '23 at 06:47
  • @Schecher_1 even `i` can be something else so as `T` teaching others how to name a variable is an utter waste of time cause they have their own style I can name it to `textlines` or `blob` and no one should have an issue with the choice of word. And yes the answer can be modified/extended for bigger file size, the current implementation will work for those 23 lines and if he comes backs to tell me the file size I will be happy to update my answer. – Vinod Srivastav Mar 27 '23 at 10:23
-1

There are a lot of ways to accomplish this. All of them involve iterating through the lines of your text file.

Here's one such solution that plays on the new ValueTuple type available in C# 7.

string path = "file path here";

Dictionary<string, (string Gender, string Weight, string BloodType)> records =
    new Dictionary<string, (string Gender, string Weight, string BloodType)>();

Stack<string> stack =
    new Stack<string>();

foreach (string line in File.ReadLines(path))
{
    if (stack.Count != 1)
    {
        stack.Push(line);
        continue;
    }

    string[] fields = 
        line.Split('*');

    records.Add(
        stack.Pop(), 
        (Gender: fields[0], 
         Weight: fields[1], 
         BloodType: fields[2]));
}

This snippet streams lines from the file one at a time. First it pushes the name line onto a stack. Once there's a name on the stack, the next loop pops it off, parses the current line for record information, and adds it all to the records dictionary using the name as a key.

While this solution will get you started, there are some obvious areas in which you can improve it's robustness with some insight into your data environment.

For example, this solution doesn't handle cases where either the name or the record information may be missing, nor does it handle the case where the record information may not have all three fields.

You should think carefully about how to handle such cases in your implementing code.

Austin Drenski
  • 506
  • 4
  • 10