Simple ASCII out to a file question. (Equiv to VB Print #x,string)

Askjerry

Member
Joined
Aug 25, 2014
Messages
10
Programming Experience
10+
I feel like such a NOOBE... been programming in C# for about 3 hours now... so I guess I am. :rapture:

Anyway... I'm stuck on what is likely C# 101 stuff... I programmed code to talk to a USB device, create a file, pull data from the device... I just want to print it to the file.

I have a textbox called "file_name" and I dropped the name/path of the file into it... and I can look at the directory and see the file is actually created...
C#:
 //----------- Put a file somewhere -----------------------------------------------------------
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Excel CSV|*.csv|All Files|*.*";
            saveFileDialog1.Title = "Save Data File";
            saveFileDialog1.ShowDialog();
            //----------- Now do something ---------------------------------------------------------------
            file_name.Text = saveFileDialog1.FileName;

That works... I'll skip the bunch of code to configure the device, etc... herre is where I am getting the data and TRYING to output the results to the file...

C#:
if (saveFileDialog1.FileName != "")
            {
                System.IO.FileStream myfile = (System.IO.FileStream)saveFileDialog1.OpenFile();
                //============================================================================================
                textBox1.Text += "Iarray Data\r\n";
                for (int myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Iarray[myCounter].ToString("D2") + ",";
                    //--- INSERT WRITE here
                }
                textBox1.Text += "\r\n";
                //============================================================================================
                textBox1.Text += "\r\n" + "Qarray Data\r\n";
                for (int myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Qarray[myCounter].ToString("D2") + ",";
                    //--- INSERT WRITE here
                }
                textBox1.Text += "\r\n";
                //============================================================================================
                myfile.Close();
            }

In the places I have "//--- INSERT WRITE here" I want to send out the string of text that is being kicked out in the line just above...

In VB I would have opened the file and done something like...
C#:
Print #1, myText

So in this case I want to do something like...

write to myfile, Qarray[myCounter].ToString("D2") + ",";

Obviously... the issue is that "write to myfile" isn't the method/function to use.... "myfile.Write()" wants a bunch of BYTE values...

I can define a string... myText... and do something like...

C#:
string myText = Iarray[myCounter].ToString("D2") + ",";

So how to I get myText out to the file????? (grrrr)

Thanks,
Jerry
 
Last edited:
Don't. It actually makes life more difficult. Just use the code formatting tags that I did, which provide syntax highlighting automatically, i.e.

[xcode=c#]your code here[/xcode]

Oh... cool! (I only knew about the [CODE] tag... )

Thanks for that!
Jerry

Let me test that...
// - Append more text
                    using (var writer = new StreamWriter(myfile,true))
                    {
                        writer.Write(Iarray[myCounter].ToString("D2") + ",");
                    }
 
I understand that... but so far... I haven't quite figured out how not to do that.
Just use a single `using` block and put everything else inside that. This:
if (saveFileDialog1.FileName != "")
{
    using (var writer = new StreamWriter(myfile))
    {
        writer.WriteLine("Data Start");
    }

    textBox1.Text += "Iarray,";

    using (var writer = new StreamWriter(myfile, true))
    {
        writer.Write("Iarray,");
    }

    for (int myCounter = 0; myCounter < 512; myCounter++)
    {
        textBox1.Text += Iarray[myCounter].ToString("D2") + ",";

        using (var writer = new StreamWriter(myfile, true))
        {
            writer.Write(Iarray[myCounter].ToString("D2") + ",");
        }
    }

    textBox1.Text += "OK";

    using (var writer = new StreamWriter(myfile, true))
    {
        writer.WriteLine("ok");
    }

    textBox1.Text += "\r\n" + "Qarray,";

    using (var writer = new StreamWriter(myfile, true))
    {
        writer.Write("Qarray,");
    }

    for (int myCounter = 0; myCounter < 512; myCounter++)
    {
        textBox1.Text += Qarray[myCounter].ToString("D2") + ",";

        using (var writer = new StreamWriter(myfile, true))
        {
            writer.Write(Qarray[myCounter].ToString("D2") + ",");
        }
    }

    textBox1.Text += "OK";

    using (var writer = new StreamWriter(myfile, true))
    {
        writer.WriteLine("ok");
    }
}
becomes this:
if (saveFileDialog1.FileName != "")
{
    using (var writer = new StreamWriter(myfile))
    {
        writer.WriteLine("Data Start");

        textBox1.Text += "Iarray,";

        writer.Write("Iarray,");

        for (int myCounter = 0; myCounter < 512; myCounter++)
        {
            textBox1.Text += Iarray[myCounter].ToString("D2") + ",";

            writer.Write(Iarray[myCounter].ToString("D2") + ",");
        }

        textBox1.Text += "OK";

        writer.WriteLine("ok");

        textBox1.Text += "\r\n" + "Qarray,";

        writer.Write("Qarray,");

        for (int myCounter = 0; myCounter < 512; myCounter++)
        {
            textBox1.Text += Qarray[myCounter].ToString("D2") + ",";

            writer.Write(Qarray[myCounter].ToString("D2") + ",");
        }

        textBox1.Text += "OK";

        writer.WriteLine("ok");
    }
}
Given that you're putting the same text in the TextBox and file, I'd be inclined to use a StringBuilder to create a single String and then assign that in one go to the Text of the TextBox and then make a single call to File.WriteAllText to write out the whole file.
 
Ok, I see... let me amend the code and tinker a bit... the next step will be to get this to automatically kick out data from an input to the device... but I have a good handle on how the print stream works... I'll condense it down... thanks!!!

Edit - I put your code in there... works perfectly. (I appreciate that!)
 
Last edited:
U could also shorten most of the stuff to (see below).
There is no need to iterate throught the array (i think)
//concatenate the Array to a string, we need some Linq here, cause the default string.Join accepts only string[], but this could be done with a separate function too, to reduce redundant code                
                string strIarrayStart = "Iarray,";
                string concatenatedIarray = string.Join(",", Iarray.Select(x => x.ToString()).ToArray());
                //default end string for a line
                string strOkay = ",ok";
                //default starting for Qarray
                string strQArrayStart = "Qarray";
                //concatenate the Array to a string
                string concatenatedQarray = string.Join(",", Qarray.Select(x => x.ToString()).ToArray());
                //textboxstring
                string strbox = strIarrayStart + concatenatedIarray + strOkay.ToUpper() + "\r\n" + strQArrayStart + concatenatedQarray + strOkay.ToUpper();
                //string to write to file
                string strFile = strIarrayStart + concatenatedIarray + strOkay.ToLower() + "\r\n" + strQArrayStart + concatenatedQarray + strOkay.ToLower();
                textBox1.Text += strbox;
                using (var writer = new StreamWriter(myfile))
                {
                    writer.Write(strFile);
                }
 
Back
Top Bottom