Say we say I have "Hello" in C:\MyFile.txt and we want add the "World" after the hello. To accomodate, I used to read everything from the file to a string, add to the string, and then write the string back to a text file like so:
using (Stream textReaderAndWriter = new FileStream(@"C:\MyFile.txt", FileMode.Open))
{
byte[] dataCapacity = new byte[0x100];
textReaderAndWriter.Read(dataCapacity, 0, dataCapacity.Length);
string concatinationGrounds = "";
foreach(char character in ASCIIEncoding.ASCII.GetString(dataCapacity))
{
if (char.IsLetterOrDigit(character))
{
concatinationGrounds += character;
}
}
concatinationGrounds = concatinationGrounds + " World";
textReaderAndWriter.Position = 0;
dataCapacity = ASCIIEncoding.ASCII.GetBytes(concatinationGrounds);
textReaderAndWriter.Write(dataCapacity, 0, dataCapacity.Length);
}
Today, I finished reading Chapter 14 of C# 4.0 in a Nutshell which is on File I/O (input/output) stuff. I thought that one of the things I learned in this chapter was this appending shortcut...
byte[] extraData = ASCIIEncoding.ASCII.GetBytes(" World");
using (Stream textAppender = new FileStream(@"C:\MyFile.txt", FileMode.Append))
{
textAppender.Write(extraData, 0, extraData.Length);
}
...but then I dug up some of my old notes and found I had done this before:
So what did I learn from this chapter?
- Flush() per the book: "forces any internally buffered data to be written immediately"
- \r is a "carriage return" and \n is a "line feed" so think of \r\n as a ReturN
- fileInfo.Attribures ^= FileAttributes.Hidden; will toggle the hidden flag from true to false or false to true for a FileInfo object
- CRC is cyclic redundancy check
- UAC stands for User Account Control and this is the Windows feature that asks you: "Do you want to allow the following program to make changes to this computer?"
- In new byte[0x100] the 0x notation is hexadecimal in which:
- 0x10 is 16
- 0x100 is 16 x 16 or 256
- 0x1000 is 16 x 16 x 16 which is some big number
- etc.
No comments:
Post a Comment