Skip to content

Infinite loop while reading file #68

@AlainBartmanDilaw

Description

@AlainBartmanDilaw

Custom.csv

The following code enters in an infinite loop on line

var lines = outlines.ToList();

Custom.csv file has been added.

Uncomment line with BufferSize makes the code run perfectly.

Loop has been detected so far at CSVFile Line 58

using CSVFile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CSVFileLoader
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var filename = @"Custom.csv";
            FileInfo f = new FileInfo(filename);

            Console.WriteLine($"File size = {f.Length}");

            CSVSettings settings = new CSVSettings
            {
                LineSeparator = "\n",
                FieldDelimiter = '\t',
                //BufferSize = (int)f.Length
            };

            var csv = CSVReader.FromFile(filename, settings);
            
            var outlines = csv.Lines();
            var lines = outlines.ToList();
            Console.WriteLine($"Real number of lines = {lines.Count}");

            // Count amount fields by line
            var fieldCountDictionary = new Dictionary<int, int>();
            foreach (var line in lines)
            {
                int fieldCount = line.Length; // Amount of fields
                if (fieldCountDictionary.ContainsKey(fieldCount))
                {
                    fieldCountDictionary[fieldCount]++;
                }
                else
                {
                    fieldCountDictionary[fieldCount] = 1;
                }
            }

            // Show number of rows by number of fields
            foreach (var kvp in fieldCountDictionary)
            {
                Console.WriteLine($"There are {kvp.Value} rows with {kvp.Key} fields");
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions