1

My objective with this code is to use the foreach loop to go through each object and write the current string value to a txt file.

I'm using "Woof" and "Bull" as a test. Bull is the string variable in my AverageValues class.

Unfortunately, it currently will not write the value of bull to the file, however, it will create the file.

I think this is something easy to fix, I'm just can't seem to find it right now.

All help would be appreciated!

    public void doStuff()
    {
        AverageValues AVS = new AverageValues();
        AVS.Bull = "Woof";
        string path = "C:\\users\\kjenks11\\Averages.txt";
        FileStream NewFile = File.Create(path);
        StreamWriter writeIt = new StreamWriter(NewFile);
        List<AverageValues> AV = new List<AverageValues>();
        AV.Add(AVS);

        foreach (var value in AV)
        {
                writeIt.Write(value.Bull);
        } 
        NewFile.Close();
     }
0

3 Answers 3

3

You need to flush to write the data to the file. You also might consider adding using statements to your writer to free the resources when you're done.

public void doStuff()
{
  AverageValues AVS = new AverageValues();
  AVS.Bull = "Woof";
  string path = "C:\\users\\kjenks11\\Averages.txt";
  using (var NewFile = File.Create(path))
  {
    using (var writeIt = new StreamWriter(NewFile))
    {
      List<AverageValues> AV = new List<AverageValues> {AVS};
      foreach (var value in AV)
      {
        writeIt.Write(value.Bull);
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Flush or close the stream before closing the file itself:

foreach (var value in AV)
{
     writeIt.WriteLine(value.Bull);
}
writeIt.Flush();
writeIt.Close();

Note on style - when creating a Stream (of any kind), or rather, any object that implements IDisposable, create it with a using statement:

using(var writeIt = new StreamWriter(NewFile))
{
  // use writeIt here - it will dispose properly
}

Comments

2

If you use using around your stream, there's no need to call flush and close explicitly. As soon as you leave the using block' scope the stream will be closed and disposed. The close will call flush for you.

  public void doStuff()
    {
        AverageValues AVS = new AverageValues();
        AVS.Bull = "Woof";
        string path = "C:\\users\\kjenks11\\Averages.txt";
        FileStream NewFile = File.Create(path);
        List<AverageValues> AV = new List<AverageValues>();
        AV.Add(AVS);

        using(StreamWriter writeIt = new StreamWriter(NewFile))
        {
           foreach (var value in AV)
           {
                writeIt.Write(value.Bull);
           } 
        }
       NewFile.Close();
    }

1 Comment

You can use using on the FileStream like @EJC post, but at minimim I'd move the NewFile.Close() to below the using block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.