简体   繁体   中英

Why does not work return C#?

I have the following code:

public static Array readCVS(string absolutePath)
{
    string[,] temperatureMatrix = new string[384, 288];
    string value;

    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        int y = 1;

        while (csv.Read())
        {
            for (int x = 1; csv.TryGetField<string>(x, out value); x++)
            {
                x = x - 1;
                temperatureMatrix[1, 1] = value;
            }
            y = y + 1;
        }
        return temperatureMatrix;
    }
}

So return t; does not work, I mean it does not return array, also I tried to set break point here, then I can not see structure of filled array

It seems that your code enters an infinite loop in the for used to read the column values of a line extracted by the CsvReader.

I think you should change your code to remove the decrement of x inside the for loop (not clear the reason for that line but surely it doesn't allow the x to advance to the next column of the input line).

Finally you should set the values of the matrix using correctly the y (for rows) and x (for columns)

public static Array readCVS(string absolutePath)
{
    string[,] temperatureMatrix = new string[384, 288];
    string value;
    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        int y = 0;
        while (csv.Read())
        {
            for (int x = 0; csv.TryGetField<string>(x, out value); x++)
                temperatureMatrix[x, y] = value;

            y = y + 1;
        }
        return temperatureMatrix;
    }
}

I have also changed the initial indexing from 1 to 0 both for x and y. In Net arrays (also the multidimensional ones) starts at index 0 not 1.

Be warned also that this code is very dependant from the exact structure of your input file. If you ever get a file with more that 288 lines or with a single line with more than 384 single temp values the code will crash with an index out of range exception.

As pointes by Steve, you code seems to be incorrect at many places. I want to point out one more thing, better to use List not Array , because if you have large file, you will get index out of range exception. and List will give you infinite length.

public static List<string> readCVS(string absolutePath)
{
    List<string> result = new List<string>();
    string value;
    using (TextReader fileReader = File.OpenText(absolutePath))
    {
        var csv = new CsvReader(fileReader);
        csv.Configuration.HasHeaderRecord = false;
        while (csv.Read())
        {
            for (int i = 0; csv.TryGetField<string>(i, out value); i++)
            {
                result.Add(value);
            }
        }
    }
    return result;
}

I dont know why were you using y in your code. It was of no use, we were just decrementing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM