Click here to Skip to main content
15,506,209 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
TextWriter tw = System.IO.File.CreateText(sOutputFilePath + sOutFileName);
    if (record == "abcd")
    {
        id = 10;
        name = "John";
        string sout = "";
        sout = id + "|" + name;
        tw.WriteLine(sout);//output in file1

    }
    else if (record == "efgh")
    {
        id = 11;
        name = "Sam";
        string sout1 = "";
        sout1 = id + "|" + name;
        tw.WriteLine(sout1); output in file2
    }
    else
    {
    }

Problem is file1 is always empty , only file2 is working correctly.

Can you please guide me.

What I have tried:

not getting idea please help me.
Posted
Updated 18-Jul-17 11:03am
v3

You need two TextWriters:
C#
TextWriter tw1 = System.IO.File.CreateText(sOutputFilePath + sOutFileName1);
...
TextWriter tw2 = System.IO.File.CreateText(sOutputFilePath + sOutFileName2);
Also don't forget to close them when you're done:
C#
tw1.Close(); 
tw2.Close();
 
Share this answer
 
v2
Comments
sai.2012 18-Jul-17 15:15pm    
Thanks for the prompt replay RickZeeland and ppolymorphe.

created two testwriters and it works fine.

Thankyou.
First of all indent your code properly, it helps reading.
C#
TextWriter tw = System.IO.File.CreateText(sOutputFilePath + sOutFileName);
if(record == "abcd")
{
	id=10;
	name="John";
	string sout ="";
	sout=id+ "|" +name;
	tw.WriteLine(sout);//output in file1

}
else if(record == "efgh")
{
	id=11;
	name="Sam";
	string sout1 ="";
	sout1 =id+ "|" +name;
	tw.WriteLine(sout1); output in file2
}
else
{
}


Use the debugger to see what your code is doing. We can't test your code as it is not an autonomous piece of code.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Even simpler to use is File.AppendAllText, see example here: [dotnetperls]
You don't have to worry about opening and closing a stream when you use this.
 
Share this answer
 
Thank you all . I have tried your solution, it worked for me.
 
Share this answer
 
Comments
Patrice T 1-Dec-17 18:42pm    
You should not use a solution just to speak to us.
Leave a message instead or update your question.

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