Click here to Skip to main content
16,010,392 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have to call this function many times

        public void Insert(string name, string DT, int scope)
        {
            FileStream fHandler = new FileStream("c:\\SymbolTable.txt", FileMode.Append);
            fHandler.Close();
            StreamWriter sEntry = new StreamWriter("c:\\SymbolTable.txt");
            sEntry.WriteLine(name + "," + DT + "," + scope);
            sEntry.Close();
}


calling method in another class
C#
HelpingFunctions hF = new HelpingFunctions();
            hF.Insert("gh", "ggh", 0);
           hF.Insert("gh", "ggh", 1);


the problem is that my file only contains the last entry
Posted

Well, yes. It will be only the last entry.
You open it for append (using fHandler) then close that and re-open it with a StreamWriter - which destroys any existing content.

Replace your entire method with:

C#
public void Insert(string name, string DT, int scope)
    {
    using (StreamWriter sw = File.AppendText("c:\\SymbolTable.txt"))
        {
        sw.WriteLine(name + "," + DT + "," + scope);
        }
    }
 
Share this answer
 
Comments
Sweety Khan 21-Dec-11 4:45am    
ohhh. Thanx :)
Sweety Khan 21-Dec-11 4:48am    
how strange. output is this gh,ggh,1
gh,ggh,0
gh,ggh,1
y 3 entries n out of order? :(
OriginalGriff 21-Dec-11 5:03am    
At a guess, you are not clearing the file content at startup (or preferably, renaming it to a backup)
Look at the File.Move and File.Delete methods.
Sweety Khan 21-Dec-11 5:12am    
thanx its done by using file.delete
Sweety Khan 21-Dec-11 4:55am    
i got the problem whenever program runs, and fn calls it append on the same file used by the last run. tell me somthing i write on the end of the program so tht it destroys
use this one :

C#
public void Insert(string name, string DT, int scope)
{
    //FileStream fHandler = new FileStream("d:\\SymbolTable.txt", FileMode.Append);
    //fHandler.Close();
    StreamWriter sEntry = new StreamWriter("d:\\SymbolTable.txt", true);
    sEntry.WriteLine(name + "," + DT + "," + scope);
    sEntry.Close();
}


Excuse me that I commented out the first two lines but I didn't get its purpose if you want to append data.

Hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900