Writing data to a text file
Solution 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.
Solution 2
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
Solution 3
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.
Arianule
Updated on June 04, 2022Comments
-
Arianule almost 2 years
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?
-
Oded over 11 yearsSimplify it you may have, answer the question you did not.
-
Mike Perrenoud over 11 years@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?
-
Oded over 11 yearsDid 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.
-
Mike Perrenoud over 11 years@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?
-
Oded over 11 yearsSure, but when someone asks why is X not working, they want to know why X is not working, not to see Y that is.
-
Mike Perrenoud over 11 years@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.
-
Oded over 11 yearsMy 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.
-
Mike Perrenoud over 11 years@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.