0

I was trying to create an array, using a .txt file in which there are words. Only one word per array. But down there, the fajlbe.ReadLine(); is pinned as wrong code. Why?

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();
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • 2
    Can you post the error, please? – Anthony Liriano Apr 29 '18 at 14:09
  • As you can see in the title, the error is "Cannot implicitly convert type 'string' to 'string[]'" – Gergely Dávid Apr 29 '18 at 14:12
  • HINT: `ReadLine` returns `string` not `string[]`. – blins Apr 29 '18 at 14:14
  • The intent is not clear... one way I read it is that you want to split each line so that you end up with an array of “words”. Also, you may just want to use `ReadAllLines()`. See https://stackoverflow.com/questions/4220993/c-sharp-how-to-convert-file-readlines-into-string-array – blins Apr 29 '18 at 14:24

1 Answers1

0
tomb = fajlbe.ReadLine();

The ReadLine method returns a string, while the variable tomb is declared as a string array (string[] tomb = new String[1];). You can't assign a string to an array of string.

Instead, if you intend to assign the string to the first element of the array, you can do that by setting the first element of the array:

tomb[0] = fajlbe.ReadLine();

It doesn't really seem like you need an array here. If that's so, you can simply change the declaration of variable tomb to declare it as a string instead:

string tomb = string.Empty;

Of course then you would need to change,

TSor[n].árucikk = tomb[0];

to,

TSor[n].árucikk = tomb;
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55