Check read/opened file for completeness with C# programming language

Sela 126 Reputation points
2022-01-25T16:32:23.597+00:00

Dear Sir or Madam,

I have at present the task an existing file with the programming language C# in the program to read and/or open. After opening the file, it should be determined whether the file was read or opened completely and correctly, I thought of something to LEX and YACC (lexical analyzer). If the data have been read/opened correctly, they are to be processed further. The reading in is not a problem. My question would be how to check the completeness of the data.
An example: I have a file with workers inside, which have their own ID etc.. Now it should be checked, if I call the employee 1 (Max Mustermann) that the ID is not a string but a number.

With kind regards,
Sela

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,262 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2022-01-25T18:10:10.423+00:00

    The following is my version of loosely writing out a path to consider which can take different directions but I like to pre-process data for validation purposes, here is just the int aspect.

    Note there is more validation like is there a value when splitting data, is the data delimited in the first place, are there blank lines etc. Yet a good reason to have validation when data is being saved to a file and a better choice for a file is json.

    So let's say the data is first created using say an Employee class, all well defined properties, this would be the first step followed by assertion e.g. if id is missing which they should never be than stop right there etc.

    public class FileOperations
    {
        private string _fileName = "TODO";
    
        public (bool success, List<Employee> list) ReadEmployees()
        {
            List<Employee> list = new();
    
            var lines = File.ReadAllLines(_fileName);
    
            var hasValidIdentifierAsIntlines = 
                lines
                    .Select(x => x.Split(','))
                    .All(x => int.TryParse(x[0], out var _));
    
    
            return hasValidIdentifierAsIntlines ? (true, Process(lines)) : (false, null);
        }
    
        private List<Employee> Process(string[] lines)
        {
            throw new NotImplementedException();
        }
    }
    
    public class Employee
    {
        public int Id { get; set; }
    }
    

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2022-01-25T18:42:15.04+00:00

    As explained in your 3 other thread on this subject, just use one of the JSON serialization libraries that comes with .NET and/or .NET Core.

    https://learn.microsoft.com/en-us/answers/questions/706223/json-parse.html
    https://learn.microsoft.com/en-us/answers/questions/699941/read-and-process-json-file-with-c.html
    https://learn.microsoft.com/en-us/answers/questions/697880/convert-json-file-to-db-file.html

    The deserialization process will throw an exception if the data types do not match. At this point, it's not clear if you followed the previous recommendations and ran into trouble or ignored the previous recommendations. Can you provide example JSON both good and bad? Can you share your current code base so we can see what you're doing?

    1 person found this answer helpful.

  2. Karen Payne MVP 35,036 Reputation points
    2022-01-25T16:38:39.177+00:00

    Use try parse, mocked up example where if an int result is the int representation of the string.

    string value = "1";
    if (int.TryParse(value, out var result))
    {
        // is an int
    }
    else
    {
        // not an int
    }
    

  3. Bruce (SqlWork.com) 56,286 Reputation points
    2022-01-25T20:11:39.493+00:00

    lex is a unix utility that helps process a context free grammar. while there're ports to windows, it is not common on windows (probably not too common on unix anymore as ast trees are more popular todays).

    you probably want to look at ANTLR

    https://github.com/antlr/antlr4/blob/4.6/doc/index.md

    there is a c# version.

    https://www.nuget.org/packages/Antlr4/
    https://github.com/antlr/antlr4/blob/master/doc/csharp-target.md

    or as suggested pick a currency supported format like json or xml, which already has formatters and schema verifier support.

    0 comments No comments