It's ya boi Jamie
It's ya boi Jamie

Reputation: 11

C# FileStream creating new .csv file but not writing to it

Hi I'm trying to write data to a .csv file. My code is creating the new file but isn't writing to it. Here is a shorter version of the code with the name, age, and sex variables already predetermined. In my actual application these variables come from textboxes (I have made sure that the data from the textboxes is actually filling these variables using a MessageBox).

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "C:\\Users\\Jamie\\Desktop\\test.csv";
            string name = "jamie";
            int age = 69;
            string sex = "male";

            if (!(File.Exists(filePath)))
            {
                FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write);
                if (fs.CanWrite)
                {
                    string columnTitles = "name,age,sex\n";
                    fs.Write(Encoding.ASCII.GetBytes(columnTitles), 0, 0);
                    string input = name + "," + age + "," + sex + "\n";
                    fs.Write(Encoding.ASCII.GetBytes(input), 0, 0);
                    fs.Flush();
                    fs.Close();
                }
            }
            else
            {
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                if (fs.CanWrite)
                {
                    if (new FileInfo(filePath).Length == 0)
                    {
                        string columnTitles = "name,age,sex\n";
                        fs.Write(Encoding.ASCII.GetBytes(columnTitles), 0, 0);
                    }
                    string input = name + "," + age + "," + sex + "\n";
                    fs.Write(Encoding.ASCII.GetBytes(input), 0, 0);
                    fs.Flush();
                    fs.Close();
                }
            }
        }
    }
}

Upvotes: 1

Views: 5291

Answers (2)

Akash KC
Akash KC

Reputation: 16310

Use following code to write :

   fs.Write(Encoding.ASCII.GetBytes(input), 0, ASCIIEncoding.ASCII.GetByteCount(input));

Third argument of FileStream Write method would need to know to maximum numbers of bytes to write. You are specifying 0 byte as maximum byte to write in your case so it's not writing to file.

Apart from your error, I would suggest to use using statement as FileStream implements IDisposable interface and it will take care of flusing stream as soon as it goes out of scope of using statement.

using(FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
{
    if (fs.CanWrite)
        {
            string columnTitles = "name,age,sex\n";
            fs.Write(Encoding.ASCII.GetBytes(columnTitles), 0, 0);
            string input = name + "," + age + "," + sex + "\n";
            fs.Write(Encoding.ASCII.GetBytes(input), 0, ASCIIEncoding.ASCII.GetByteCount(input));
        }
}

Upvotes: 1

Sean Mitchell
Sean Mitchell

Reputation: 457

The Problem:

I first tested your code and after some time working on it I discovered where you made your mistake. Specifically you failed to tell fs.Write() the number of bytes to write by only telling it 0. Therefore it entered nothing.

The Solution:

To fix this the third parameter, a integer, called count within the method should be set to they byte[]'s length.

Other Options:

You may want to take a look at System.IO.StreamWriter it will make your life much easier when it comes to writing strings to files.

Have a nice day!

Upvotes: 0

Related Questions