Skip to main content
 首页 » 编程设计

C#从逗号分隔的文本文件中读取到二维数组

2024年02月20日11kuangbin

我目前有这个:

using (StreamReader sr = new StreamReader("answers.txt")) 
{ 
    for (iCountLine = 0; iCountLine < 10; iCountLine++) 
    { 
         for (iCountAnswer = 0; iCountAnswer < 4; iCountAnswer++) 
         { 
             sQuestionAnswers[iCountLine, iCountAnswer] =  
         } 
    } 
} 

我的文本文件格式如下(10 行文本,每行有 4 个项目,用逗号分隔):

example, example, example, example  
123, 123, 123, 123 

我不确定在 for 循环中的“=”之后我需要什么来让它读取文本文件的内容并将其拆分为二维数组。

请您参考如下方法:

I'm not sure what I need after the "=" in the for loop

上面还少了一行:

var tokens = sr.ReadLine().Split(','); 

现在带有 = 的行看起来像这样:

sQuestionAnswers[iCountLine, iCountAnswer] = tokens[iCountAnswer];