Hi,
I have an csv file attached to my script and one of the columns in the file is YEAR. I use 'vehYear' as variable to bind to 'YEAR' column. I want to create a file called TestResult.txt and write the content of the variable 'vehYear' to the TestResult.txt using StreamWriter.WriteLine.Somehow I cannot get the value of the variable written to the TestResult.txt file. I debugged it, and y is getting the correct value in the Writeline line...but TestResult.txt doesn't seem to get printed the value of y. It prints only the string
"Answer is "
This is my code:
using (StreamWriter myWriter = new StreamWriter("C:\\...\\testresult.txt",true))
{
string y = year.ToString();
result = " Answer is ";
myWriter.WriteLine(result, y);
}
my Output in TestResult.txt file is :- Answer is
Thanks for any help,
Jenny
Writing to a text file using StremWriter
Re: Writing to a text file using StremWriter
Try this:
Or, if you want to use the formatted version do this:
Formatted statements require place-holders in the form "{<index>}", where index is a 0-based index of the list of objects following the format string in the method. In this case, y is the 0th value in the list of values after the format string 'result'.
Code: Select all
myWriter.WriteLine(result + y);
Code: Select all
result = "Answer is {0}";
myWriter.WriteLine(result, y);
Shortcuts usually aren't...
Re: Writing to a text file using StremWriter
Hi,
Somehow it didn't work with Writeline..But it did work with
myWriter.Writer(result +y);
Thanks!
Somehow it didn't work with Writeline..But it did work with
myWriter.Writer(result +y);
Thanks!
Re: Writing to a text file using StremWriter
I mean
myWriter.Write(result +y);
myWriter.Write(result +y);
Re: Writing to a text file using StremWriter
Hmm... It should work with either one, they do the same thing except for the end-of-line characters.
Write puts the given string into the current cursor location, without adding the end-of-line characters.
WriteLine puts the given string into the current cursor location AND adds the end-of-line characters.
Anyway, glad it helped.
Write puts the given string into the current cursor location, without adding the end-of-line characters.
WriteLine puts the given string into the current cursor location AND adds the end-of-line characters.
Anyway, glad it helped.
Shortcuts usually aren't...