Writing a txt File problem

Ok, in my proyect iam trying to simulate a drawing editor, but i want to create a Log File ( drawing.txt )

in this file i want that every line have two points example:

{100,100},{200,00} // this for the first line drawn

{45,88},{82,89} // this for the second line ...

//... etc, for every line drawn

so i did in my code a function, fucntion that is called by the event OnPaint

public void logactions(Point a, Point b)

{

string path_string = "C:\\Drawing1.txt";

using (StreamWriter _path = new StreamWriter(path_string))

{

string data = a.ToString() + "," + b.ToString();

_path.WriteLine(data);

_path.Close();

}

}

this code create succesfully the first line, but this line is overwriten by the next line, and this for the next ... etc...

how can i create this kind of custom log , please help me




Answer this question

Writing a txt File problem

  • SEK05

    Finally, if Im wrong, please let me know... if Im ok, so i hope this post should help you ...

    // Adding in the form class

    FileStream _file;

    // OnLoad

    private void FlatPlane_Load(object sender, EventArgs e)

    {

    if (!File.Exists(path))

    {

    // Create the file.

    using (_file = File.Open(path, FileMode.Append))

    {

    }

    }

    }

    //MouseUp

    private void FlatPlane_MouseUp(object sender, MouseEventArgs e)

    {

    //si fue del izq

    if (e.Button == MouseButtons.Left)

    {

    b.X = e.X;

    b.Y = e.Y;

    this.LogIt(a, b);

    this.Invalidate();

    }

    Pinta = false;

    }

    // Log the activity

    public void LogIt(Point a, Point b)

    {

    _file = File.Open(path, FileMode.Append);

    Byte[] info = new UTF8Encoding(true).GetBytes(a.ToString() + "," + b.ToString() + "\n");

    // Add some information to the file.

    _file.Write(info, 0, info.Length);

    _file.Dispose();

    }



  • Paul Arena

    1/ OnPaint is called constantly, this is the wrong place to be writing to a log file, you have no control over it

    2/ You're creating a new file every time, that's why it's overriting. There's a File.Open shortcut in 2.0, but any option for opening a file has an overload where you can specify if you want to create a new file or append an old one

    3/ For something like this, I'd open the file as a member variable, rather than have the cost of opening and closing it constantly



  • The VB

    Cool, so now the Code in the OnLoad E/M looks like this...

    // OnLoad

    private void FlatPlane_Load(object sender, EventArgs e)

    {

    if (!File.Exists(path))

    {

    // Create the file.

    using (FileStream _file = File.Open(path,FileMode.Create))

    {

    _file.Close();

    }

    }

    else

    {

    File.Open(path, FileMode.Append);

    }

    }

    but im still don't know how to write it, but I will figure it out, thank you so much...



  • Alexandrei

    Yep, sounds good, except you can use File.Open ( from memory ) and append the file instead of worrying about overwriting it.



  • Qumber Abbas

    using (FileStream _file = File.Open(path,FileMode.Create))

    {

    _file.Close();

    }

    No, in both cases, you're not storing the file as a member variable. In one, you create a temp variable, in the other you open the file and don't store the return value at all. You want to open it to a *member* variable in both cases, then you write to it where-ever you like, and make sure it's closed in your Dispose/Finalise methods.



  • WarauInu

    Rod Fraga wrote:

    this code create succesfully the first line, but this line is overwriten by the next line, and this for the next ... etc...

    how can i create this kind of custom log , please help me

    Sorry, the output or the drawing1.txt file have the following...

    {234,585},{456,785} // this is the las Line drawn in the form so where are the previous lines



  • saderogba

    Your onload call is a total waste of time. What you've done will work, but it still opens the file every time you want to write to it.

    _file = File.Open(path, FileMode.Append);

    This should be in your Load event, and the Dispose call should be replaced by Close, and THEN Dispose, and then set the variable to NULL, and this should take place in the Close event of the form, assuming this is in a form.



  • ourti

    sorry.. ok i got it

  • robolt

    Ok, thx, then I must create the file outside the OnPaint Event/Method, and I will place it inside the OnLoad Event/Method... that's cool, so now the "Log Activity" I will place it inside the OnMouseUp Event/Method so in this case I have the control and I will log the activity when i place the last point ... I'm Ok

    But now i have a new question:

    If I use in the OnLoad E/M

    if (!File.Exists(path))
    {
    // Create the file.
    using (FileStream _file = File.Create(path))
    {
    // Add some cool stuff

    }
    }

    now how can I write the txt file in the OnMouseUp E/M



  • Writing a txt File problem