-5
using System;
using System.IO;

namespace Sudoku
{
    class Game
    {
        private int[,] puzzle = new int[9, 9];

        public void saveToFile()
        {
            StreamWriter str = new StreamWriter("SUDOKU.txt");
            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 9; ++j)
                {
                    str.Write(puzzle[i, j] + " ");
                }
                str.Write("\t\n");
            }
            str.Close();
        }

        public void readFromFile()
        {
            clear();
            StreamReader str = new StreamReader("SUDOKU.txt");
            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 9; ++j)
                {
                    puzzle[i, j] = Convert.ToInt32(str.Read());
                }
            }
            str.Close();
        }
    }
}

I can not download the data from the file. Saving works fine and has a view of the txt file:

1 2 3 4 5 6 7 8 9 
4 5 6 7 8 9 1 2 3 
7 8 9 1 2 3 4 5 6 
2 1 4 3 6 5 8 9 7 
3 6 5 8 9 7 2 1 4 
8 9 7 2 1 4 3 6 5 
5 3 1 6 4 2 9 7 8 
6 4 2 9 7 8 5 3 1 
9 7 8 5 3 1 6 4 2 

How it all written in my array 9x9 skipping all the gaps that would be all the data is written correctly?

  • 5
    `str.Read()` does not do what you thing it does. Read the documentation. – SLaks Nov 12 '14 at 01:04
  • I ask you to tell me how to do it, and do not read the documentation – user3239481 Nov 12 '14 at 01:05
  • @user3239481 I'm hoping there's a language barrier here. Are you saying that you don't care to read documentation when we'll do it for you? – Matthew Haugen Nov 12 '14 at 01:07
  • 1
    @user3239481: you don't want to learn and read documentation - hire someone to do that for you. – zerkms Nov 12 '14 at 01:07
  • 1
    `user3239481` your comment definitely won't get you any help..it's not our job to tell you how to do something.. if you are stuck on something and do not want to read the documentation and or show more effort..then I guess you're on your own.. show some more effort.. – MethodMan Nov 12 '14 at 01:07
  • 1
    You cannot learn to write code without reading documentation. – SLaks Nov 12 '14 at 01:08
  • 3
    Yeah, I wrote an answer that did it for you, but I deleted it upon reading that comment, pending this getting worked out. – Matthew Haugen Nov 12 '14 at 01:09
  • I used to when I explain. If read, it makes no sense, I do not understand. That's why you turned to what would have shown on the example. – user3239481 Nov 12 '14 at 01:09
  • 1
    @user3239481 perhaps you should go find a chat forum or a friend where you could discuss in order to learn a bit more. It also sometimes helps to take the sample code from the documentation and then change it to do something a bit different until you get a good understanding. – miltonb Nov 12 '14 at 01:12
  • 1
    @user3239481: start from the beginning - what does `StreamReader.Read()` return? – zerkms Nov 12 '14 at 01:12
  • here is a super / simple site that even a 4th grader can understand in regards to it's easy reading .. read this and try some of the samples to fit your use case.. and most of all stop being `LAZY` I don't know or I don't understand is not an excuse..`My dog ate my HomeWork Too` but the teacher still made me take the Pop Quiz.. http://www.dotnetperls.com/streamreader – MethodMan Nov 12 '14 at 01:14

1 Answers1

0

Instead of using str.Read() which would require you to read single characters (or a buffer that you specified), try using str.Readline() to read a single line for each iteration of i.

public void readFromFile()
{
    StreamReader str = new StreamReader("SUDOKU.txt");
    for (int i = 0; i < 9; ++i)
    {
        string[] lineNumbers = str.ReadLine().Split(' ');
        for (int j = 0; j < 9; ++j)
        {
            puzzle[i, j] = Convert.ToInt32(lineNumbers[j]);
        }
    }
    str.Close();
}

This reads a single line at a time (each iteration of i), splits the line into lineNumbers by separating the current line by space characters. Each number on the current line can then be accessed by lineNumbers[j] within your inner loop (each iteration of j).

grovesNL
  • 6,016
  • 2
  • 20
  • 32