954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Best method to create text file

Hello
There seems to be different methods to create a text file in C#.

I am getting a byte array from USB device. I need to store this as text file which is to be read, updated, and saved for future usage.

The data comes in different packets not in a single stretch.
which is the most suitable way for this purpose.


Thank you

Roy Thomas

RoyMicro
Light Poster
40 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

If you intend to parse or convert the byte[] into its string equivalents, the StreamWriter should work fine as long as long as it does not need to be thread safe. For thread safety, look at TextWriter .

If you just want to write the byte[] to a file, look at FileStream class.

If you intend to parse the byte[] array into various types, consider using the BinaryWriter class.

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

For text file access i use StreamReader and StreamWriter objects. You can use them to write a new line for each packet of data as it is recieved:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}


Code came from msdn .

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

As DdoubleD rightly pointed out, there are different methods that are useful for different situations, so it comes down to which tool fits the job :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Thanks for the reply. Got an idea to start with. I think I will need to use different methods on different occasions for convenience.
btw If I want to create the file (text file) in the current folder where my exe is running, how should I pass the file name argument in filestream instantiation?
I am relatively new to C# (Windows programming in general). I am basically a Microcontroller programmer who needs some windows application development skills.
Thanks
Roy Thomas

RoyMicro
Light Poster
40 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

Here is a method that uses the FileStream object, but you can also use the StreamWriter object to write data to a text file. The code below creates the file if it doesn't exist, then writes the data to the text file. If the file already exist you will use File.Append. Some of this code is from the Wrox 2005 C# book. Hope this helps.

public static void FileStreamOpenOrCreate(string path, char[] charData)
        {

            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);

            byte[] byteData = new byte[charData.Length];

            Encoder e = Encoding.UTF8.GetEncoder();

            e.GetBytes(charData, 0, charData.Length, byteData, 0, true);

            fs.Seek(0, SeekOrigin.Begin);

            fs.Write(byteData, 0, byteData.Length);

            fs.Dispose();

        }
sanch01r
Light Poster
30 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 
If I want to create the file (text file) in the current folder where my exe is running, how should I pass the file name argument in filestream instantiation? Thanks Roy Thomas

Hi Roy. If you pass in just the filename (no path), the file will be created in thesame folder as where your application is running, which is the current working directory. The only caveat to this is, if your application's process changes the current working directory.

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

Thanks a lot. Almost clear. The text file is ok now in my application.
I have to move on to other processing once the file is full and ready.

Thanks a lot.
Roy Thomas

RoyMicro
Light Poster
40 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

glad you have it going :) if your question has been answered, rememeber to mark the thread as sovled.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: