【发布时间】:2018-04-29 22:20:30
【问题描述】:
我试图创建一个数组,使用一个包含单词的 .txt 文件。每个数组只有一个字。但是在那里,fajlbe.ReadLine(); 被固定为错误代码。为什么?
struct Sor
{
public string árucikk;
}
static Sor[] TSor = new Sor[1000];
static void Main(string[] args)
{
StreamReader fajlbe = new StreamReader("penztar.txt");
string[] tomb = new String[1];
int n = 0;
while(!fajlbe.EndOfStream)
{
tomb = fajlbe.ReadLine(); //here I've got an error
TSor[n].árucikk = tomb[0];
n++;
}
Console.WriteLine(TSor[1]);
fajlbe.Close();
【问题讨论】:
-
你能把错误贴出来吗?
-
如你在标题中看到的,错误是“Cannot implicitly convert type 'string' to 'string[]'”
-
提示:
ReadLine返回string而不是string[]。 -
意图不明确......我读它的一种方式是你想分割每一行,这样你就得到了一个“单词”数组。此外,您可能只想使用
ReadAllLines()。见stackoverflow.com/questions/4220993/…