Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C为什么在解析CSV或TSV文件时只能得到部分结果?_C#_Visual Studio 2010_Csv_Split_Generic List - Fatal编程技术网

C# C为什么在解析CSV或TSV文件时只能得到部分结果?

C# C为什么在解析CSV或TSV文件时只能得到部分结果?,c#,visual-studio-2010,csv,split,generic-list,C#,Visual Studio 2010,Csv,Split,Generic List,我试图从一个包含100行的CSV文件中获取第二个值。我得到前42个值,然后它停止。。。没有错误消息,也没有错误处理。我很困惑,我在一个时间表上。它也对TSV文件进行了测试,但给出了前43个结果。如果你觉得奇怪,请帮忙告诉我 我正在使用streamreader,将每一行读入一个字符串数组,拆分数组并获取第二个值并将其添加到列表中 string path = @"C:\Users\dave\Desktop\codes\testfile.txt"; StreamRead

我试图从一个包含100行的CSV文件中获取第二个值。我得到前42个值,然后它停止。。。没有错误消息,也没有错误处理。我很困惑,我在一个时间表上。它也对TSV文件进行了测试,但给出了前43个结果。如果你觉得奇怪,请帮忙告诉我

我正在使用streamreader,将每一行读入一个字符串数组,拆分数组并获取第二个值并将其添加到列表中

        string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
        StreamReader sr = new StreamReader(path);
        List<string> stkno = new List<string>();

        foreach (var line in path)
        {
            string s = sr.ReadLine();
            string[] words = s.Split(',');
            stkno.Add(words[1]);
        }
        var message = string.Join(",", stkno.ToArray());
        MessageBox.Show(message);
path变量是一个字符串。这意味着,当你开始讨论它时,你会得到一系列字符-‘C’然后’:‘然后’\’等等。我不认为这就是你想要做的

下面是一个更简单的方法,使用:

或:


如果您使用的是.NET 3.5,并且不介意一次性读取整个文件,则可以使用file.ReadAllLines来代替。

您无意中迭代了文件路径中的字符数,而不是字符串中的行数。此更改应解决以下问题:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    StreamReader sr = new StreamReader(path);
    List<string> stkno = new List<string>();

    while (sr.Peek() >= 0) 
    {
        string s = sr.ReadLine();
        string[] words = s.Split(',');
        stkno.Add(words[1]);
    }
    var message = string.Join(",", stkno.ToArray());
    MessageBox.Show(message);
这个怎么样:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    var secondWords = from line in File.ReadAllLines(path)
                        let words = line.Split(',')
                        select words[1];

    var message = string.Join(",", secondWords.ToArray());

我想你的意思是:

    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    StreamReader sr = new StreamReader(path);
    List<string> stkno = new List<string>();

    string s;
    while(s = sr.ReadLine() != null)
    {
        string[] words = s.Split(',');
        stkno.Add(words[1]);
    }
    var message = string.Join(",", stkno.ToArray());
    MessageBox.Show(message);

没有例外吗?如果你跨过这个循环怎么办?@Dave_P-请帮你自己一个忙,不要用这个字符串来获取文件的路径。请简单地获取当前用户的桌面路径并从那里开始工作。你也可以考虑把文件本地化到应用程序本身。为什么你给我一个-1在这个?你摇滚。谢谢你的帮助。我曾经尝试过使用peek,但也许我没有先重新构建解决方案。问题解决了。和天才们在一起真是太好了。我个人不喜欢这样使用Peek-调用ReadLine并查看它是否返回null更干净,依我看。还要注意这里没有using语句-StreamReader永远不会关闭。将File.ReadLines与foreach结合使用的另一个优点。。。不需要显式清理。@Jon Skeet,我完全同意你的观点,并建议按照你展示的方式来实现它。我只是想让海报的代码保持相似。实际上,它是在读取文件中的行。。。它只是停留在42%。Matt Manela帮我解决了问题,谢谢。@Dave_P:您正在读取文件中的行,但只是当路径中有更多字符时-这决定了您读取的行数。我相信我的迭代方法比显式使用StreamReader更简洁。
    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    var secondWords = from line in File.ReadAllLines(path)
                        let words = line.Split(',')
                        select words[1];

    var message = string.Join(",", secondWords.ToArray());
    string path = @"C:\Users\dave\Desktop\codes\testfile.txt";
    StreamReader sr = new StreamReader(path);
    List<string> stkno = new List<string>();

    string s;
    while(s = sr.ReadLine() != null)
    {
        string[] words = s.Split(',');
        stkno.Add(words[1]);
    }
    var message = string.Join(",", stkno.ToArray());
    MessageBox.Show(message);