all groups > dotnet clr > july 2007 >
You're in the

dotnet clr

group:

Read and write to Console or a file


Read and write to Console or a file Allen Maki
7/20/2007 4:19:40 PM
dotnet clr:


How come line 1 will output garbage to the screen, but line 2 will output a
good number to a file?

How can I make line 1 out put a good number to the screen







String* dayStr = System::DateTime::Now.ToString("dd");

Int32 dayInt = System::Int32::Parse(dayStr);

Console::WriteLine("the day is {0}", __box(dayInt) ); // Line 1



.

.

.





String* path = new String("_1D.txt"); // will read the time from a file

FileStream * fs = new FileStream(path, FileMode::Open);

BinaryReade* br = new BinaryReader(fs);

Int32 day = br->ReadInt32();



fs = new FileStream(S"_1k.txt", FileMode::Create); //will write the time to
another file

BinaryWriter* bw = new BinaryWriter(fs);

Br->Write( day);
//Line 2




















Re: Read and write to Console or a file Göran_Andersson
7/21/2007 7:25:56 PM
[quoted text, click to view]

What kind of "garbage"? The wrong number? Random characters? An exception?

[quoted text, click to view]

Why do you create a string and parse it? Why not simply:

Int32 dayInt = System::DateTime::Now::Day;

[quoted text, click to view]

You forgot to close the stream here.

[quoted text, click to view]

--
Göran Andersson
_____
Re: Read and write to Console or a file Allen Maki
7/21/2007 10:23:44 PM
Hi Göran,



I really appreciate your help. I learn a new way to deal with DateTime
which is (Int32 CurHour = System::DateTime::Now.Hour), but now I have a
problem.



Format1(your format): Int32 dayInt = System::DateTime::Now.Day;




Format 2 (The one I use before): Int32 dayInt = System::Int32::Parse(dd);

Int32
CurHour = System::DateTime::Now.Hour;



Now, after I use format 2 instead of format 1, the things went to the
opposite, which is I get garbage (random character) when I write to files
and get good numbers when writing to the screen.



To answer your questions:



Why do you create a string and parse it?



The answer is, that was the only way for me to get good number into the
file.



What kind of "garbage"? The wrong number? Random characters? An exception?



The answer is Before I was getting wrong numbers.






[quoted text, click to view]

AddThis Social Bookmark Button