i have created a marksheet progra开发者_StackOverflow中文版m in c# 2005, its working fine it is taking input and showing the output by using the below code. now if i want that the output of this program is also copied in a new file located in my C or any drive. what should i do? i tried IO.StreamWriter but its not working.
using(System.IO.TextWriter writer = System.IO.File.CreateText(@"C:\1.txt"))
{
Console.WriteLine("\t\tMarksheet - Prislogix Public School");
Console.Write("\n\nStudent Name : " + name);
Console.Write("\nClass : " + cls);
Console.Write("\nRoll Number : " + roll);
Console.Write("\n\nSubject\t\tObtained Marks\t\tTotal Marks");
Console.Write("\n\nChemistry\t\t" + chem + "\t\t75");
Console.Write("\nEnglish\t\t\t" + eng + "\t\t100");
Console.Write("\nCalculus\t\t\t" + urd + "\t\t100");
Console.Write("\nDiscrete\t\t\t" + sin + "\t\t75");
Console.Write("\nMathematics\t\t" + mat + "\t\t100");
Console.Write("\nPhysics\t\t\t" + phy + "\t\t75");
Console.Write("\nComputer\t\t" + comp + "\t\t100");
Console.Write("\nMethods\t\t" + isl + "\t\t50");
float tot = chem + eng + urd + sin + mat + phy + comp + isl;
Console.Write("\n\n\t\t\tTotal Obtained Marks : " + tot + "\tOut Of 625");
float per;
per = (tot / 625) * 100;
Console.Write("\n\t\t\tPercentage : " + per + "%");
if (per < 49 && per > 40)
{
Console.Write("\n\t\t\tFAILED!");
}
if (per <= 59 && per >= 50)
{
Console.Write("\n\t\t\tGrade : C");
}
if (per <= 69 && per >= 60)
{
Console.Write("\n\t\t\tGrade : B");
}
if (per <= 79 && per >= 70)
{
Console.Write("\n\t\t\tGrade : A");
}
if (per <= 89 && per >= 80)
{
Console.Write("\n\t\t\tGrade : A+");
}
if (per <= 100 && per >= 90)
{
Console.Write("\n\t\t\tGrade : A-ONE");
}
}
}
Console.ReadLine();
StreamWriter
does work. It's a shame you haven't shown us your attempt at using it.
The simplest way of creating a StreamWriter
to write to a file is to use File.CreateText
. Then you should be able to use StreamWriter.Write
in the same way as Console.Write
. Don't forget to use a using
statement:
using (TextWriter writer = File.CreateText("..."))
{
// Write stuff here
}
That makes sure the writer will be disposed at the end of the using
statement, which will flush and close the file.
If you want to write to both the console and the file, you may want a "tee" TextWriter
which writes to two outputs... or you could just look at the file after running the program.
One thing to note: in Windows, the normal line ending is \r\n
rather than just \n
. If the problem was just that all the output looked like it was on one line in Notepad, that's probably the issue. Consider using WriteLine
instead of Write
, to write out the platform default line terminator at the end of the line.
If all of this fails, please tell us what's going wrong with rather more detail than "it's not working".
Change all calls to Console.Write so that they write to a StringBuilder instance instead. After the printing is done you can do the following:
Console.Write(stringBuilder.ToString());
System.IO.File.WriteAllText("your path here", stringBuilder.ToString());
This will probably be the easiest fix. It will still write the output to the Console, but it will also write to the file you want.
精彩评论