3

C # - Read from the CSV file producing two entries at a time?

 3 years ago
source link: https://www.codesd.com/item/c-read-from-the-csv-file-producing-two-entries-at-a-time.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C # - Read from the CSV file producing two entries at a time?

advertisements

I am trying to read in a list of several hundred thousand values from what once was a spreadsheet, but for the sake of simplicity, I have turned into a CSV file.

My problem is that while testing it to make sure it reads properly, the code is for some reason ignoring the comma after the second position, and combining the value in that spot with the value next to it, despite being, you know, separated by a comma. It also begins to combine the final value with the first value from the next set.

For Example:

CSV File:

0,0,0,104672
0,1,6,51971
0,1,36,80212
0,2,5,51972
0,2,13,51973
...

Program Output:

0
00
1046720
00
16
519710
136
...

I think the example probably does a better job describing what's going on than I did in words. It continues like that, displaying the wrong information until it reaches the end of the file.

My code is as follows:

static void Main()
{
    using(var fs = File.OpenRead(@"C:\path\to\file.csv"))
    using(var read = new StreamReader(fs))
    {
        while (!read.EndOfStream)
        {
            int i = 0;
            var line = read.ReadLine();
            while (i < 4)
            {
                var values = line.Split(',');
                Console.Write(values[i]);
                Console.Read();
                i++;
            }
        }
    }
}

EDIT: Sorry, I got lost in my understanding of what the code should do and forgot to explain the goal here.

This program is made to take these values and rename a file from the 4th value (for example, 104672) to the first three values, separated by dashes (ex. 0-0-0). What I want from my output right now is to be able to see the program give me the values back, one at a time, so that I know when I go to rename the files, I'm not getting improper results.

EDIT 2: I also realize, a day later, that the answer I got was one of significance to making my program work, rather than actually discovering why I was getting the output I got. For those curious in the future, the answer is essentially that Console.Read(); is not a true pause, and causes more writes to happen upon key press than expected.


if you need those values for later use, why don't you put them into List for later... like this:

List<string[]> listOfValues = new List<string[]>();
using (var fs = File.OpenRead(@"C:\temp\csv.txt"))
    using (var read = new StreamReader(fs))
    {
        while (!read.EndOfStream)
        {
            var line = read.ReadLine();
            listOfValues.Add(line.Split(','));
        }
    }

later, you can use data from list:

for (int i = 0; i<listOfValues.Count; i++)
{
    Console.WriteLine("line number: {0}, contents: {1}", i + 1, string.Join(" ", listOfValues[i]));
}

which gives you

line number: 1, contents: 0 0 0 104672
line number: 2, contents: 0 1 6 51971
line number: 3, contents: 0 1 36 80212
line number: 4, contents: 0 2 5 51972
line number: 5, contents: 0 2 13 51973

Tags csv

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK