C# 获取文件每行中的数字之和

C# 获取文件每行中的数字之和,c#,streamreader,C#,Streamreader,我有一个文件,其中有一些数字,我想在每行中加上数字1和数字2。以下是我文件中的数字: -944 -857 -158 356 540 70 15 148 例如,我想求-944和-857的和,我该怎么办?? 我按照下面的代码来检查数字是多少,输出是-158和15(它没有显示-944和540!!!): 您在while条件下执行一条读线,然后又在while作用域的主体中执行一条读线,因此跳过了1条读线指令(在while条件下)。请尝试以下方法: StreamReader ar = new Stre

我有一个文件,其中有一些数字,我想在每行中加上数字1和数字2。以下是我文件中的数字:

-944 -857
-158 356
 540 70
 15 148
例如,我想求-944和-857的和,我该怎么办?? 我按照下面的代码来检查数字是多少,输出是-158和15(它没有显示-944和540!!!):


您在while条件下执行一条读线,然后又在while作用域的主体中执行一条读线,因此跳过了1条读线指令(在while条件下)。请尝试以下方法:

StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
string s = ar.ReadLine();
while (s != null)
{
    //string[] spl = s.Split(' ');
    // below code seems safer, blatantly copied from one of the other answers..
    string[] spl = s.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
    MessageBox.Show(spl[0]);
    s = ar.ReadLine();
 }
试试这个(简化版): 您正在阅读两次,但仅使用第二次阅读


注意:您也没有从行的开头修剪空格,因此如果数据有前导空格(如示例所示),您将丢失数字。

您在检查时读取
中的一行,然后再次解析该值-这就是它似乎只读取偶数行的原因

提议的解决办法:

StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
// Prepare the first line.
string line = ar.ReadLine();
while (line != null) {
    // http://msdn.microsoft.com/en-us/library/1bwe3zdy.aspx
    string[] spl = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
    MessageBox.Show(spl[0]);
    // Prepare the next line.
    line = ar.ReadLine();
}

更新:使用
string.Split()
重载,该重载不返回空结果,最多返回2个值(1和字符串的其余部分)。

我在某种程度上假设它们正确缩进(即未缩进)。但是很好。@HiTech我不确定原始输入是否有那个前导空间。OP声称已经放映了15部,所以修剪不是他的问题。不显示
-944
540
可以用双
ReadLine()
来解释。@HiTect我假设这是因为OP每次读取两次,因此跳过一行,并且每行只显示第一个数字。我放弃了我以前的评论,因为这可能只是他的格式(然而,修剪线条始终是一种良好的做法)。+1抱歉(我看到你添加了一个修剪):)我在这里假设了一个理想的输入,他的输入列表中出现的额外空格只是为了一个可读性好的列表而应用的额外格式,在这种情况下,修剪是多余的。我放弃了我以前的评论,因为这可能只是他的格式(不过修剪线条总是一个很好的做法)。+1作为道歉:)没关系,疯狂的假设可能是万恶之源:-)无论如何,我会对它进行编辑,不管怎样,这会让我投票选出最佳解决方案,以便更好地使用StringSplitOptions.RemoveEmptyEntries解决任何格式问题。我会将line=ar.ReadLine()滚动到while子句中:)
    StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
    while ((string line = ar.ReadLine()) != null)
    {
        string[] spl = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
        MessageBox.Show(spl[0]);
    }
StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
// Prepare the first line.
string line = ar.ReadLine();
while (line != null) {
    // http://msdn.microsoft.com/en-us/library/1bwe3zdy.aspx
    string[] spl = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
    MessageBox.Show(spl[0]);
    // Prepare the next line.
    line = ar.ReadLine();
}
    StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
    // Load a line into s
    string s = ar.ReadLine();
    while (s != null)
    {
        // Split on space
        string[] spl = s.Trim(' ').Split(' ');
        // Declare two variables to hold the numbers
        int one;
        int two;
        // Try to parse the strings into the numbers and display the sum
        if ( int.TryParse( spl[0], out one ) && int.TryParse( spl[1], out two) ) {
            MessageBox.Show( (one + two).ToString() )
        }
        // Error if the parsing failed
        else {
            MessageBox.Show("Error: Numbers were not in integer format");
        }
        // Read the next line into s
        s = ar.ReadLine();

    }