2

I have a simple program where I write 6 of 7 numbers to a text file. Logically everything seems to be fine.

However the numbers are not written to the file as expected.

Random random = new Random();

Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
//creating the lotto file
FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bs = new BufferedStream(fs);
Console.WriteLine("File created");
fs.Close();
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");

for(int i = 0; i < 6; i++)
{
    for(int j = 0; j < 7; j++)
    {
        //Console.Write(random.Next(1, 49));
        sw.Write(random.Next(1, 49) + " " );

    }
    sw.WriteLine();

}
sw.Close();

The file was created, however no numbers were written to the file...advice perhaps as to why?

Arianule
  • 8,811
  • 45
  • 116
  • 174

4 Answers4

1

What are you trying to do? Whay you declare so many streams for nothing? Just use:

using(StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt")){
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 7; j++)
        {
            //Console.Write(random.Next(1, 49));
            sw.Write(random.Next(1, 49) + " " );

    }
    Console.WriteLine();
    }
}

http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx

Oscar
  • 13,594
  • 8
  • 47
  • 75
1

Note that your code is not optimized and has a lot of unnecessary streams and buffers being created but the answer by @Michael outlines the right code to use in it's place. My answer will just highlight why your code wasn't working in the intended way.

The answer to your question is actually very simple.

StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");

You have forgotten to add the / in the string to ../... If fileLotto is assumed to have the value example then the FileStream will create the file example.txt but the StreamWriter will access ..example.txt for writing and that too in a different folder.

Use variables to define values that have to be repeated used. Remember the DRY principle.

Random random = new Random();

Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
string fileName = "../../" + fileLotto + ".txt";
//creating the lotto file
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
Console.WriteLine("File created");
fs.Close();

StreamWriter sw = new StreamWriter(fileName);

for(int i = 0; i < 6; i++)
{
    for(int j = 0; j < 7; j++)
    {
        //Console.Write(random.Next(1, 49));
        sw.Write(random.Next(1, 49) + " " );

    }
    Console.WriteLine();

}
sw.Close();

Again I say please use @Michael's code. This is just to highlight the primary issue with your code.

Tanzeel Kazi
  • 3,797
  • 1
  • 17
  • 22
1

Well I have to admit that this is not a fancy code. But for why this is not working is this
In this line

FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);

You are opening file in "../../" folder which is two up folder of executable file.
But in this line

StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");

Same parameter is "../.." which causes another file to be opened parent folder of executable with ".." in the beginnig of file name. You have add an extra '/' at the end of StreamWriter parameter to ensure you are writing the first file you created using FileStream.

Yakup Ünyılmaz
  • 404
  • 3
  • 11
0

Let's simplify this:

Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();

StringBuilder text = new StringBuilder();
for(int i = 0; i < 6; i++)
{
    for(int j = 0; j < 7; j++)
    {
        text.Append(random.Next(1, 49) + " " );
    }
    Console.WriteLine();
}

File.WriteAllText(string.Format("../../{0}.txt", fileLotto), text.ToString());

This code is also safer. You're not opening a bunch of streams (that you aren't closing BTW) that are unnecessary. Rather you are getting all of the text together and writing it all at once.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Simplify it you may have, answer the question you did not. – Oded Dec 28 '12 at 12:39
  • @Oded, does not this code work (unlike the OP's) and at the same time simplify *and* add a layer of safety because there's not a ridiculous number of random streams open that aren't getting close either? – Mike Perrenoud Dec 28 '12 at 12:41
  • Did you not see the comment by Jon Skeet - the code by the OP _does_ work for him. And the question was _why_ doesn't their code work, not how to make it simpler. – Oded Dec 28 '12 at 12:41
  • @Oded, I'm confused a bit. Many times (as I'm sure you would agree) the process of getting code to work is a process of refactoring and learning new things. Better ways of doing the same thing. Yes? Is that not considered an answer to a question? Also, I just saw Jon's comment, but it's clearly not working for the OP, and **likely** because the OP isn't *flushing*, but the fact is the code they have is unstable yes? So shouldn't they use a better way? – Mike Perrenoud Dec 28 '12 at 12:44
  • Sure, but when someone asks _why_ is X not working, they want to know why X is not working, not to see Y that is. – Oded Dec 28 '12 at 12:45
  • @Oded, hmm. But if our job here is to simply enable people to continue with bad habits, how have we contributed to the community? Does it matter why the code doesn't work if a refactoring that shows the OP a better way works? Maybe I'm missing the point. I will gladly delete my answer as your reputation clearly shows you know more about this community than I do, but I thought I was helping by showing the OP a better way. – Mike Perrenoud Dec 28 '12 at 12:50
  • My point was more - explain why it isn't working, _then_ show a better way. As it stands, the OP will not understand the bad habits and why they are bad. – Oded Dec 28 '12 at 12:53
  • @Oded, understood. Apparently the code worked for the OP as they have accepted my answer, and honestly when I placed the old code locally it worked for me, but next time I'll work the OP through why the code they are using needs to be modified so they can learn from it. – Mike Perrenoud Dec 28 '12 at 13:00