locked
Read file RRS feed

  • Question

  • Hello there,

    I got a small question. I have a methode that is reading a txt file.. This txt file obtains numbers like this: 0 0 0 2 0 0 4 0 0 1  ect,

    Is it possible to add every number to a list. 

    Check my methode ( and list)

    List<Label> lbl = new List<Label>();
    
    
    public void Sudoku_reader()
            {
                //Create a FileInfo instance representing an existing text file.
                FileInfo MyFile = new FileInfo(@Sudoku_txt);
                //Instantiate a StreamReader to read from the text file.
                StreamReader sr = MyFile.OpenText();
                    char[] buffer = new char[1024];
                    int read;
                    while ((read = sr.ReadBlock(buffer, 0, buffer.Length)) > 0)
                    {
                        for (int i = 0; i < read; i++)
                           MessageBox.Show(Convert.ToString( buffer[i]));
                    }
    
                sr.Close();
                Generate_Field();
            }

    I hope you understand my question.

    Pasibun

    Monday, June 2, 2014 1:27 PM

Answers

  • Hi,

    You can read the entire file and the split your line using the space separator.

    List<Label> lbl = File.ReadAllText(@Sudoku_txt).Split(" ").Select(x => new Label{ Text = x }).ToList();


    Kevin BEAUGRAND, Modis FRANCE
    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont résolu votre problème.

    • Proposed as answer by Magnus (MM8)MVP Monday, June 2, 2014 1:36 PM
    • Marked as answer by Pasibun Monday, June 2, 2014 7:53 PM
    Monday, June 2, 2014 1:33 PM

All replies

  • Hi,

    You can read the entire file and the split your line using the space separator.

    List<Label> lbl = File.ReadAllText(@Sudoku_txt).Split(" ").Select(x => new Label{ Text = x }).ToList();


    Kevin BEAUGRAND, Modis FRANCE
    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont résolu votre problème.

    • Proposed as answer by Magnus (MM8)MVP Monday, June 2, 2014 1:36 PM
    • Marked as answer by Pasibun Monday, June 2, 2014 7:53 PM
    Monday, June 2, 2014 1:33 PM
  • Must i add this in the for loop or is that not necessary?
    Monday, June 2, 2014 2:07 PM
  • No, 

    Loops are not necessary. The given line does everything.


    Kevin BEAUGRAND, Modis FRANCE
    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont résolu votre problème.

    Monday, June 2, 2014 2:08 PM
  • I get two errors with this code:

    Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments

    Error 2 Argument 1: cannot convert from 'string' to 'char[]'

    Monday, June 2, 2014 2:21 PM
  • So, 

    Don't use double quote string : 

    List<Label> lbl = File.ReadAllText(@Sudoku_txt).Split(' ').Select(x => new Label{ Text = x }).ToList();

    Sorry,


    Kevin BEAUGRAND, Modis FRANCE
    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont résolu votre problème.

    • Marked as answer by Pasibun Monday, June 2, 2014 2:38 PM
    • Unmarked as answer by Pasibun Monday, June 2, 2014 7:10 PM
    Monday, June 2, 2014 2:23 PM
  • One more question.

            public void Generate_Field()
            {
                Controls.Add(groupBox1);
                groupBox1.AutoSize = true;
                for (int i = 0; i < grote; i++)
                {
                    lbl.Add(new Label());
                    lbl[i].Text = (i + 1).ToString();
                    lbl[i].Font = new Font("Tahoma", 12.0F);
                    lbl[i].AutoSize = true;
                    lbl[i].Top = top;
                    lbl[i].Left = left;
                    groupBox1.Controls.Add(lbl[i]);
                    left += 25;
                    if (((i + 1) % 9) == 0)
                    {
                        top += 20;
                        left = 10;
                    }
                }
            }

    The methode above will generate a field.

    What i want to create is that the input of the txt file should be reflected in the field.

    The next methode the user can select a txt file:

     private void Load_Sudoku_Click(object sender, EventArgs e)
            {
                dlg.InitialDirectory = "c:\\";
                dlg.Filter = "txt files (*.txt)|*.txt";
                dlg.FilterIndex = 2;
                dlg.RestoreDirectory = true;
                MessageBox.Show("Let op! Bij het inladen van een nieuwe sudoku moet je er rekening meer houden dat er na elke cijfer een: , moet zitten. Klein voorbeeld:    0, 0, 0, 4, 0, 5, 0, 0, 0 ");
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = dlg.OpenFile()) != null)
                        {
                            using (myStream)
                            {
                                Sudoku_txt = dlg.FileName;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }
                List<Label> lbl = File.ReadAllText(Sudoku_txt).Split(' ').Select(x => new Label { Text = x }).ToList();
                Generate_Field();
            }

    At the add i have add you line of code and atfer that he must generate the field.. but my output is:

    Hope you understand my problem.


    Monday, June 2, 2014 2:45 PM