Welcome to Dream.In.Code
Click Here
Getting C# Help is Easy!

Join 117,165 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 2,525 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Need advice which File Functions should I use?

2 Pages V  1 2 >  
Reply to this topicStart new topic

Need advice which File Functions should I use?

turtleC++
post 1 Jul, 2008 - 09:48 AM
Post #1


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

I just know the basic writing DLL and C#, and now i'm thinking of making logFile DLL writen in C# because that way I don't have to re-implement over and over again whenever I want to report my results to the file. With DLL, I just need to reference it and call its functions. C# has alot of functions that can write to a file, but I don't know which one is best for this project.

1. I want this DLL has a function that can create new log file. If file exists, it will delete it then create new one in text file, .txt.
2. After file is created, there should be functions that allow to write string, double, integer, char, bool and so on.


So what file functions should I use, have any suggestion?
User is offlineProfile CardPM

Go to the top of the page


zakary
post 1 Jul, 2008 - 09:52 AM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


create your own function and use System.IO.StreamWriter, System.IO.Directory, and System.IO.File to create and write to your txt file
User is online!Profile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 09:55 AM
Post #3


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

CODE

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;//For FileStream.


namespace LogFileResult
{
    public class TestLog
    {
        private static FileStream File;
        private static StreamWriter WriteStr;


        //Constructor.
        public TestLog()
        {
            File = new FileStream("TestLog.txt", FileMode.CreateNew, FileAccess.ReadWrite);
            WriteStr = new StreamWriter(File);
        }


        //Create file.
        public void CreateTestLog()
        {
            //TextWriter tw = new StreamWriter("TestingTestingWithData.txt");
            //tw.Close();


            //            FileInfo fi = new FileInfo(@"C:\myTesting.txt");
            //            FileStream fstr = fi.Create();

        }


        //Write string.
        public void LogString(string str)
        {
            WriteStr.WriteLine(str);
        }

        //Write char.
        public void LogChar(char ch)
        {


        }


        //Write bool.
        public void LogBoolen(bool bl)
        {


        }


        //Write int.
        public void LogInt(int numb)
        {


        }


        public void EndLog()
        {
            WriteStr.Close();
        }

    }
}



This is what I got so far, but not sure if there is better function than streamwriter..

This post has been edited by turtleC++: 1 Jul, 2008 - 09:55 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Jul, 2008 - 09:55 AM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,604



Thanked 115 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


I agree with Zak... but throw in FileStream class in there to since you will feed that class to the StreamWriter class constructor.

Streamwriter is the most versatile and offers write as well as writeline which both can be quite handy.

smile.gif

This post has been edited by Martyr2: 1 Jul, 2008 - 09:56 AM
User is offlineProfile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 09:57 AM
Post #5


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

QUOTE(Martyr2 @ 1 Jul, 2008 - 09:55 AM) *

I agree with Zak... but throw in FileStream class in there to since you will feed that class to the StreamWriter class constructor.

Streamwriter is the most versatile and offers write as well as writeline which both can be quite handy.

smile.gif



what did you mean Throw in FileStream class?

This post has been edited by turtleC++: 1 Jul, 2008 - 09:58 AM
User is offlineProfile CardPM

Go to the top of the page

zakary
post 1 Jul, 2008 - 10:43 AM
Post #6


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


he is saying to add that to the list i gave up FileStream allows you to add line after line to one file.
User is online!Profile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 11:27 AM
Post #7


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

Oh ok. Well I use FileStream in my project, but not sure whether it's same the way as he suggested or not.

So far I use streamWriter to write string, but for the string to append to the file, you have to close after finished, but how can I open streamWriter again if want to add new string to existing the file since I close it already?
User is offlineProfile CardPM

Go to the top of the page

zakary
post 1 Jul, 2008 - 11:49 AM
Post #8


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


here is an example of use of FileStream http://msdn.microsoft.com/en-us/library/sy...filestream.aspx
User is online!Profile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 11:56 AM
Post #9


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

QUOTE(zakary @ 1 Jul, 2008 - 11:49 AM) *

here is an example of use of FileStream http://msdn.microsoft.com/en-us/library/sy...filestream.aspx



Yep I looked at this exampler earlier, but this example only shows one time writing to file. What if I'm writing to file then close out the file, then later want to write more to file using streamWriter. I just don't know to open streamWriter after closing it.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 1 Jul, 2008 - 12:12 PM
Post #10


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 382



Thanked 4 times

Dream Kudos: 175
My Contributions


you can modify that code so where it has
csharp

if (File.Exists(path))
{
File.Delete(path);
}

you can use
csharp

if (File.Exists(path))
{
File.Open(path);
}


and then you can add text to that open file
User is online!Profile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 12:31 PM
Post #11


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

QUOTE(zakary @ 1 Jul, 2008 - 12:12 PM) *

you can modify that code so where it has
csharp

if (File.Exists(path))
{
File.Delete(path);
}

you can use
csharp

if (File.Exists(path))
{
File.Open(path);
}


and then you can add text to that open file


Thanks zakary for your helps. I made changes to my code from my first post.
CODE

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;




namespace TestLogFile
{
    public class TestLog
    {

        private FileStream fStream;
        private StreamWriter strmWriter;
        private string filePath;


        //Function Name:  Constructor.
        //Purpose: To create new log file.  If log file is already exist then it
        //          will get deleted first before create new log file.
        //Parameter: The function that references this DLL have to provide the path
        //              to where the file can be written.
        public TestLog(string path)
        {
            filePath = path;

            //Delete file if it exists.
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            //Create new test log file.
            fStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite);            
            //Create a writer and also open a file.
            strmWriter = new StreamWriter(fStream);

            //Close out the file and StreamWriter.
            EndLog();
        }


        //
        //
        public void CreateLog()
        {


        }



        //Function Name: LogString.
        //
        //
        public void LogString(bool writeSameLine, string str)
        {

            if(writeSameLine == true)
            {
                strmWriter.Write(str);
            }
            else
            {
                strmWriter.WriteLine(str);
            }
        }


        //Function Name: LogChar.
        //
        //
        public void LogChar(bool writeSameLine, char ch)
        {
            if(writeSameLine == true)
            {
                strmWriter.Write(ch);
            }
            else
            {
                strmWriter.WriteLine(ch);
            }
        }


        //Function Name: LogBoolen.
        //Purpose: To write True or False or other case such is 0 or 1.
        //
        public void LogBoolen(bool writeSameLine, char writeType, bool TrueFalse)
        {
        

        }


        //Function Name: LogInt.
        //
        //
        public void LogInt(int numb)
        {


        }


        //Function Name: OpenLog.
        //
        //
        public void OpenLog()
        {
            if (File.Exists(filePath))
                File.Open(filePath, FileMode.Append);
        }



        //Function Name: EndLog.
        //
        //
        public void EndLog()
        {
            strmWriter.Close();
            fStream.Close();
        }

    }
}



Here is my console app that call the code above.
CODE

using System;
using System.Collections.Generic;
using System.Text;
using TestLogFile;

namespace TestingTestingCallingTestLog
{
    class Program
    {
        public static string FilePath = @"C:\\Results\\TestLog.txt";

        static void Main(string[] args)
        {
            TestLog testClass = new TestLog(FilePath);

            testClass.LogString(false, "here I am testing testing testing");
            testClass.EndLog();


            testClass.LogChar(true, 'a');
            testClass.EndLog();

        }
    }
}


As you can see my code above, it will close the streamWriter and file everytime I finish writing, so so far I'm able to open file, but when I try to logString() again, it gives me error since my streamWriter is already closed. I can avoid this if I don't close streamWriter away, but I would like for it to close every writing session.
I want to open streamWriter in my function called openLog(), but I haven't found the way how to do that yet.

This post has been edited by turtleC++: 1 Jul, 2008 - 12:32 PM
User is offlineProfile CardPM

Go to the top of the page

turtleC++
post 1 Jul, 2008 - 02:30 PM
Post #12


D.I.C Head

**
Joined: 7 May, 2008
Posts: 51

Hey Zakary, thanks for your help. I finally get it to work now. I changed my streamWriter to be local instead in global also The StreamWriter function has another hidden argument which can be used to apply whether to write over the existing file or append to it.

CODE

StreamWriter strmWriter = new StreamWriter(filePath, true);


it works correctly so far.

This post has been edited by turtleC++: 1 Jul, 2008 - 02:32 PM
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 11:21AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month