none
Writing to a file RRS feed

  • Question

  • I worked out how to write to a file and that works fine. I.e. if I want to write the value of  to a file for 50 computations of X. It does do this, however as the contents being wrote to file is X (and X is used throughout, changing value after each computation), each Write of X overwrites the previous X results obtained from the computation, so at the end you only have one value of X in the file, the value of X for the last computation. How do I do it so that it writes to file teh vaeu of all 50 computatiosn for X without overwriting the previous 49 of the same name? 
    Monday, November 17, 2008 11:50 PM

Answers

  • Dink,

     I assume you want to write 50 different values in sequential order?

    If so, this is what I used

    For i = 1 to 50
        X = Computation...
        File.WriteLine("C:\Data.txt", i, X )
    EndFor

    Mind you, you need an existing text file at the location with at least 50 lines already in it.
    • Marked as answer by Dink87522 Tuesday, November 18, 2008 2:59 PM
    Tuesday, November 18, 2008 4:21 AM

All replies

  • hmm, you may want to save to file after each computations or like make some big string where you add them like in queue, I am not sure if it works but you may be able to use +=, but not sure as haven't used it much and more using C#
    Monday, November 17, 2008 11:56 PM
  • Dink,

     I assume you want to write 50 different values in sequential order?

    If so, this is what I used

    For i = 1 to 50
        X = Computation...
        File.WriteLine("C:\Data.txt", i, X )
    EndFor

    Mind you, you need an existing text file at the location with at least 50 lines already in it.
    • Marked as answer by Dink87522 Tuesday, November 18, 2008 2:59 PM
    Tuesday, November 18, 2008 4:21 AM
  • Pacolaco said:

    Dink,

     I assume you want to write 50 different values in sequential order?

    If so, this is what I used

    For i = 1 to 50
        X = Computation...
        File.WriteLine("C:\Data.txt", i, X )
    EndFor

    Mind you, you need an existing text file at the location with at least 50 lines already in it.


    Thanks I got it working now. My problem was only having 2 variables not 3 in File.Write and that I was also using a blank text file, not one with number lines already added.

    Why did you use File.WriteLine instead of File.WriteContents ?
    Tuesday, November 18, 2008 2:58 PM
  • I didn't use file.WriteContents because I have never used it before, and am not familiar with it. Also, the method I have above is the first thing that came to mind for me.
    Tuesday, November 18, 2008 5:26 PM