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:
If you want to write text to a file then don't create a FileStream, which is a connection to a file at the base binary level. To write text, create a StreamWriter. It is used to write text to a Stream. It can write to any type of Stream, including a FileStream. While you can, you normally wouldn't create the FileStream first though, because you can simply pass a file path to the constructor and it will handle the FileStream internally. Once you have an open StreamWriter object, you can call any of the many Write and/or WriteLine overloads to write formatted or unformatted text. Be sure to close the StreamWriter when you're done, for which you should use a `using` block if you can, e.g.
using (var writer = new StreamWriter(filePath))
{
    // Use writer here.
}
 
So if I understand you... then it's okay to use this part to get the user's request for where to put the file...
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;

But use the Streamwriter to make the connection...

C#:
using (var writer = new StreamWriter(saveFileDialog1.FileName))

{
    // Use writer here.
}
let me give that a try and see what happens... thanks,
Jerry
 
I was getting errors... then I realized that for the "using" parameter... I needed to add...

using System.IO; to the top of the code.... pressing on... one step closer.

So here is the code...
C#:
if (saveFileDialog1.FileName != "")
            {
                //============================================================================================
                textBox1.Text += "Iarray,";
                for (int myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Iarray[myCounter].ToString("D2") + ",";
                    using (FileStream fs = new FileStream(saveFileDialog1.FileName,FileMode.OpenOrCreate))
                    {
                        using (StreamWriter writer = new StreamWriter(fs))
                        {
                            writer.Write(Iarray[myCounter].ToString("D2") + ",");
                        }
                    }
                }
                textBox1.Text += "OK";
                using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))
                {
                    using (StreamWriter writer = new StreamWriter(fs))
                    {
                        writer.Write("OK");
                    }
                }
                //============================================================================================
                textBox1.Text += "\r\n" + "Qarray,";
                for (int myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Qarray[myCounter].ToString("D2") + ",";
                    using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))
                    {
                        using (StreamWriter writer = new StreamWriter(fs))
                        {
                            writer.Write(Qarray[myCounter].ToString("D2") + ",");
                        }
                    }
                }
                textBox1.Text += "OK";
                using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))
                {
                    using (StreamWriter writer = new StreamWriter(fs))
                    {
                        writer.Write("OK");
                    }
                }
                //============================================================================================
            }

When I run it... I only get a few characters... but it SHOULD echo what is in the textbox... in other words... I SHOULD get...
Iarray,-12,-09,-16,07,29,-14,-26,-08,01,10,-06,-04,02,-04,-03,-04,10,-24,13,33,-09,-14,01,-01,-04,-02,13,08,18,06,-08,-09,09,-03,03,14,30,07,14,03,-11,-27,-21,10,14,17,-08,-16,-23,-13,-02,11,15,09,-17,-25,-13,24,10,-15,-06,05,-16,-13,02,-23,-15,-15,-10,-17,00,16,28,14,01,-06,-07,-16,06,10,11,-02,-05,20,-09,02,10,-14,-03,30,31,07,14,04,-04,-11,-05,-09,-07,-04,00,-21,-05,-01,02,04,-07,-10,-14,-01,-07,04,17,-06,-03,29,08,-02,02,-01,03,03,06,23,08,-28,00,04,08,10,08,-10,00,03,12,07,-18,-12,13,19,12,34,10,-05,-02,16,-01,-09,10,40,02,-33,-07,05,00,-07,08,25,02,-08,-20,-08,01,-16,01,05,-05,04,-05,-12,-01,12,18,-07,15,-10,-06,13,04,16,-05,-24,-01,-07,15,07,-09,-01,00,04,-08,01,-05,02,23,07,-16,-27,-05,12,10,-04,11,17,-01,-05,-13,-10,10,10,04,-11,-05,12,23,23,01,-12,-14,-11,00,11,10,03,15,06,05,00,-03,-04,22,15,01,-17,-24,-09,-17,-16,05,10,26,-17,-05,-08,00,04,16,-17,-04,21,-04,-18,-14,-22,04,17,-02,29,13,-21,03,-03,28,13,-17,-29,-05,24,13,05,07,-01,09,-09,-13,23,23,-03,-07,00,06,-01,06,-13,-01,-24,-06,01,-09,29,18,-02,22,32,02,03,-02,13,-20,-04,10,-05,05,36,06,-11,01,-08,08,05,10,05,08,21,13,-08,01,-18,-17,23,15,16,21,-15,-01,26,23,16,-08,-13,-17,-14,04,04,-29,-17,-23,-05,-03,-05,05,-17,-05,-01,05,01,-08,-04,05,04,-21,-14,14,-10,03,-14,16,01,-25,-08,-01,00,15,-07,-12,07,02,-13,04,04,-11,-05,12,29,-04,-22,-11,12,-05,-28,07,-01,-09,04,-13,-13,23,18,08,10,-05,14,08,-09,11,-07,14,23,01,09,04,16,04,-07,-16,-13,15,-10,04,06,01,-09,-25,-02,-28,11,38,-09,03,36,02,-26,04,06,09,30,17,00,13,15,-18,-17,-19,09,-07,-02,08,-07,06,32,16,25,-01,02,-09,04,11,-03,21,03,02,-18,-10,07,17,04,-06,15,17,08,-12,-01,11,-22,-11,-12,-02,10,-25,-18,04,24,05,-23,-04,-17,-06,-17,-24,08,19,-27,-18,13,10,-04,-02,-25,02,14,02,-07,02,06,00,-02,-03,03,07,-13,-28,-08,30,03,04,07,01,-02,-09,-05,08,33,OK
Qarray,28,-03,-07,-21,14,11,-04,06,-29,-01,35,09,-18,-06,01,28,15,02,-04,-05,28,01,-21,09,01,06,-03,-21,-09,06,-07,-08,-28,-08,31,32,09,-14,-09,04,-02,-35,-24,-09,-03,-01,07,-17,-12,05,06,22,-09,-19,-04,19,-05,-08,12,16,-03,-11,-14,-32,-44,-29,-02,19,00,-18,-09,-12,-23,-04,-01,-01,14,-08,-16,13,00,-07,-13,-09,-20,04,-02,00,-17,08,20,-01,-02,-23,07,25,28,-19,-05,05,-09,12,04,00,-01,-07,-22,-07,-01,26,-04,-11,-05,02,21,13,-10,-06,-08,36,40,11,-10,16,23,29,-01,-08,-02,-14,-06,11,-07,-07,-01,-01,-09,-19,-11,10,27,17,-22,-02,16,08,12,06,15,35,12,19,-06,14,04,-11,14,16,12,13,00,28,30,19,08,-08,-16,-01,-05,07,19,31,28,-03,-22,-15,16,-01,-04,05,09,08,-11,-05,-01,07,-02,-07,01,06,15,02,-13,-01,27,22,-05,-08,-01,05,01,-11,-16,07,-05,07,30,13,-13,-07,03,-09,-03,09,-09,-05,09,12,-04,-12,16,26,06,-11,03,01,14,30,-05,-22,00,07,06,-11,-21,09,-14,-18,-20,-11,03,-03,01,04,-10,-15,-09,05,10,-17,10,00,11,02,09,-09,-07,17,15,03,-15,-05,19,-04,-05,-13,-09,23,08,-11,-05,-03,-03,-09,06,-01,-18,-12,-03,-21,-09,08,09,15,-29,-02,-07,-20,-10,-29,05,14,07,04,09,18,13,-09,05,16,09,09,13,33,04,-30,-07,13,06,00,08,06,14,15,-14,-17,02,10,02,-24,-08,18,-06,-17,-05,19,05,-14,09,09,09,03,16,-06,-02,21,07,08,12,03,-33,-16,05,-02,01,15,12,-11,01,03,-05,-10,03,-04,-21,-16,12,21,12,-18,-22,12,09,02,-21,-06,19,-10,06,-20,-29,-07,09,-15,-28,-23,-04,17,17,07,-18,07,03,-10,04,09,-13,-02,19,10,-14,-07,-30,-10,15,-04,-13,-20,00,13,10,-07,-06,-08,35,22,-10,13,26,10,-07,-01,11,18,-18,15,-08,-12,23,00,13,16,11,06,00,-03,17,14,-30,-31,08,13,-01,12,11,-21,-07,11,12,12,00,05,22,13,-05,-02,18,13,-04,13,-03,-12,13,17,-20,-25,14,20,21,09,-11,19,30,-15,-17,-18,-19,-11,08,10,-18,-04,23,32,18,06,12,28,-07,-02,11,08,-17,-31,-06,21,06,-19,-24,10,17,-23,-01,31,08,-08,-06,17,05,-18,-27,-05,-01,03,10,09,14,02,-19,12,12,11,OK

But what I actually get in the created file is...

I have no clue why it's not working.:numbness:
 
Last edited:
I was getting errors... then I realized that for the "using" parameter... I needed to add...

using System.IO; to the top of the code.... pressing on... one step closer.

That doesn't really have anything to do with the `using` statement to create the StreamWriter. It's wholly and solely that, when referring to a type in C# code, you must either qualify the type with its namespace or else import that namespace at the file level.
 
I didn't see your post... I added more details above... the StreamWriter is working... at least it's not failing... but it is not outputting the expected data.
 
I didn't see your post... I added more details above... the StreamWriter is working... at least it's not failing... but it is not outputting the expected data.

Have you debugged the code? Don't just read it; watch in action. Step through it line by line and make sure that it does what you expect at each step and that each expression evaluates to what you expect. As soon as the reality differs from the expectation, you know you've found an issue and then you can analyse why with some actually useful information.
 
That's what I find confusing... I'm running the operations in parallel...

Update Textbox;
Update File;
...
...
Update Textbox;
Update File;
...
...

Then... the textbox has something completely different than the file... the textbox is perfect... but the file only contains a few characters.
I could generate a textbox full of data... then output the entire textbox as one long string... but I'm trying to learn the output method properly... and I think that it is somehow writing text, then obliterating it and writing over it. Remember... my entire C# experience is less than 12 hours at this point... trying (against odds) to develop an APP by Thursday.
 
I imagine that the way you're opening the FileStream overwrites the existing file. If you want to append then you probably have to specify that explicitly.

While what you're doing is not wrong exactly, why create the FileStream first and then create the StreamWriter when you could just create a StreamWriter and pass it the file path?
 
So put this in once...
C#:
using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))

Then... just use this...

C#:
using (StreamWriter writer = new StreamWriter(fs))
                        {
                            writer.Write(somestring);
                        }

Where needed? I'll try that...
 
So put this in once...
C#:
using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate))

Then... just use this...

C#:
using (StreamWriter writer = new StreamWriter(fs))
                        {
                            writer.Write(somestring);
                        }

Where needed? I'll try that...

No! You're not listening. DO NOT create a FileStream at all. Just create the StreamWriter. If you intend to write to the file multiple times in quick succession then just open it once, e.g.
using (var writer = new StreamWriter(filePath))
{
    writer.Write("...");

    // ...

    writer.WriteLine("...");

    // ...

    writer.Write("...");
}
If you intend close the file between writes then make sure that you specify that you want to append when you open it for the second and subsequent times, e.g.
// Overwrite an existing file the first time.
using (var writer = new StreamWriter(filePath))
{
    writer.WriteLine("...");
}

// ...

// Append to the existing file the second time.
using (var writer = new StreamWriter(filePath, true))
{
    writer.WriteLine("...");
}
 
If I get knocked in the head long enough... it eventually sticks. :friendly_wink:
I really appreciate your help... and patience... it worked perfectly this time...

C#:
[COLOR=#0000ff]if [/COLOR](saveFileDialog1.FileName != [COLOR=#a52a2a]""[/COLOR])
            {
                [COLOR=#008000]//============================================================================================
                // - Overwrite existing file data (First time)[/COLOR]
                [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = [COLOR=#0000ff]new[/COLOR] [COLOR=#008080]StreamWriter[/COLOR](myfile))
                {
                    writer.WriteLine([COLOR=#a52a2a]"Data Start"[/COLOR]);
                }
                [COLOR=#008000]//============================================================================================[/COLOR]
                textBox1.Text += [COLOR=#a52a2a]"Iarray,"[/COLOR];
                [COLOR=#008000]// - Append more text[/COLOR]
                [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = new [COLOR=#008080]StreamWriter[/COLOR](myfile, [COLOR=#0000ff]true[/COLOR]))
                {
                    writer.Write([COLOR=#a52a2a]"Iarray,"[/COLOR]);
                }
               [COLOR=#0000ff] for [/COLOR]([COLOR=#0000ff]int[/COLOR] myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Iarray[myCounter].ToString([COLOR=#a52a2a]"D2"[/COLOR]) + [COLOR=#a52a2a]","[/COLOR];
                    [COLOR=#008000]// - Append more text[/COLOR]
                    [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = new [COLOR=#008080]StreamWriter[/COLOR](myfile,true))
                    {
                        writer.Write(Iarray[myCounter].ToString([COLOR=#a52a2a]"D2"[/COLOR]) + [COLOR=#a52a2a]","[/COLOR]);
                    }
                }
                textBox1.Text += [COLOR=#a52a2a]"OK"[/COLOR];
                [COLOR=#0000ff]using [/COLOR]([COLOR=#0000ff]var[/COLOR] writer = new [COLOR=#008080]StreamWriter[/COLOR](myfile, [COLOR=#0000ff]true[/COLOR]))
                {
                    writer.WriteLine([COLOR=#a52a2a]"ok"[/COLOR]);
                }
                [COLOR=#008000]//============================================================================================[/COLOR]
                textBox1.Text += [COLOR=#a52a2a]"\r\n"[/COLOR] + [COLOR=#a52a2a]"Qarray,"[/COLOR];
              [COLOR=#008000]  // - Append more text[/COLOR]
                [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = [COLOR=#0000ff]new[/COLOR] [COLOR=#008080]StreamWriter[/COLOR](myfile, [COLOR=#0000ff]true[/COLOR]))
                {
                    writer.Write([COLOR=#a52a2a]"Qarray,"[/COLOR]);
                }
                [COLOR=#0000ff]for[/COLOR] ([COLOR=#0000ff]int[/COLOR] myCounter = 0; myCounter < 512; myCounter++)
                {
                    textBox1.Text += Qarray[myCounter].ToString([COLOR=#a52a2a]"D2"[/COLOR]) + [COLOR=#a52a2a]","[/COLOR];
                    [COLOR=#008000]// - Append more text[/COLOR]
                    [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = new [COLOR=#008080]StreamWriter[/COLOR](myfile, [COLOR=#0000ff]true[/COLOR]))
                    {
                        writer.Write(Qarray[myCounter].ToString([COLOR=#a52a2a]"D2"[/COLOR]) + [COLOR=#a52a2a]","[/COLOR]);
                    }
                }
                textBox1.Text += [COLOR=#a52a2a]"OK"[/COLOR];
                [COLOR=#0000ff]using[/COLOR] ([COLOR=#0000ff]var[/COLOR] writer = new [COLOR=#008080]StreamWriter[/COLOR](myfile, true))
                {
                    writer.WriteLine([COLOR=#a52a2a]"ok"[/COLOR]);
                }
                [COLOR=#008000]//============================================================================================[/COLOR]
            }

Later I'll look at making a subroutine to condense it further... but for now... it processes data... so I can continue.
Thanks again!
Jerry

...and yes... I am OCD enough to hand color all the code... :-/
 
If this is just a learning exercise then you can obviously do whatever you need to to learn what you want but, in a real app, you certainly wouldn't keep opening and closing the same file like that, especially inside a loop. You may already realise that but I thought I'd put it out there just in case.
 
...and yes... I am OCD enough to hand color all the code... :-/

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]
 
I understand that... but so far... I haven't quite figured out how not to do that... if I can make this application work... albeit sloppy... I can bid on a job to collect data and compile into a report... at some future date... I can clean it up and resell as a professional application. But for now... I need the data more than anything else...

The equipment and software used to do this job no longer exist... and I was quoted $12,000.00 by someone to write this application.

So far my total investment is about 20 hours of my time... so I call that a win.

Thanks,
Jerry
 
Back
Top Bottom