Microsoft Visual C++ .NET(c) Step by Step

To

Do this

Write text to a file.

Create a StreamWriter that outputs to a FileStream, and then use the Write and WriteLine members of StreamWriter. For example:

FileStream* fs = new FileStream(S"foo.txt", FileMode::Append); StreamWriter* sw = new StreamWriter(fs); sw->WriteLine(S"Some text");

Flush and close the StreamWriter when you’re finished with it. For example:

sw->Flush(); sw->Close();

Read text from a file.

Create a StreamReader that reads from a FileStream, and then use the ReadLine member of StreamReader. For example:

FileStream* fs = new FileStream(S"foo.txt", FileMode::Open); StreamReader* sr = new StreamReader(fs); String* line = sr->ReadLine();

Write binary values to a file.

Create a BinaryWriter that outputs to a FileStream, and then use the overloaded Write members of BinaryWriter. For example:

FileStream* fs = new FileStream(S"bar.dat", FileMode::Create); BinaryWriter* bw = new BinaryWriter(fs); bw->Write(S"Some text"); bw->Write(100.00);

Read binary values from a file.

Create a BinaryReader that reads from a FileStream, and then use the ReadXxx members of BinaryReader. For example:

FileStream* fs = new FileStream(S"foo.txt"); BinaryReader* br = new BinaryReader(fs); String* line = br->ReadString(); Double d = br->ReadDouble();

Find out informtion about a file.

Use the static functions provided by the File class. If you’re going to perform several operations on the same file, consider creating a FileInfo object and using that instead.

Find out information about a directory.

Use the static functions provided by the Directory class. If you’re going to perform several operations on the same file, consider creating a DirectoryInfo object and using that instead.

Категории