|
/**
/ Author : Nishant S
/ EMail : nishforever@vsnl.com
/ Date : June 27, 2002
/ Info : Demo app for Code Project article
**/
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#include <windows.h>
#include <conio.h>
using namespace System;
using namespace System::IO;
__gc class FileStuffDemo
{
public:
void FileInfoDemo()
{
TCHAR WinPath[MAX_PATH+1];
GetWindowsDirectory(WinPath,MAX_PATH+1);
String* NotepadPath = WinPath;
NotepadPath = String::Concat(NotepadPath,S"\\notepad.exe");
FileInfo* finfo = new FileInfo(NotepadPath);
Show("Name",finfo->Name);
Show("Directory",finfo->Directory);
Show("Extension",finfo->Extension);
Show("Length",__box(finfo->Length));
Show("FullName",finfo->FullName);
Show("CreationTime ",finfo->CreationTime.ToString());
}
void DirectoryInfoDemo1()
{
DirectoryInfo* dinfo = new DirectoryInfo("C:\\");
DirectoryInfo* subdirs[] = dinfo->GetDirectories();
Console::WriteLine("Sub-Directories for C:\\");
Console::WriteLine("-----------------------");
for(int i=0; i<subdirs->Length; i++)
Show((i+1).ToString(),subdirs->Item[i]);
}
void DirectoryInfoDemo2()
{
DirectoryInfo* dinfo = new DirectoryInfo("C:\\");
FileInfo* filesindir[] = dinfo->GetFiles();
Console::WriteLine("Files under C:\\");
Console::WriteLine("---------------");
for(int i=0; i<filesindir->Length; i++)
Show((i+1).ToString(),filesindir->Item[i]);
}
void BinaryWriterDemo()
{
Console::WriteLine("Creating C:\\nish.com");
Console::WriteLine("This will output an 'A' to the console");
FileStream* fs = new FileStream("C:\\nish.com",
FileMode::Create,FileAccess::Write);
BinaryWriter* bw = new BinaryWriter(fs);
unsigned char SomeBinaryData __gc[] = {0xb4,0x02,0xb2,0x41,0xcd,0x21,0xcd,0x20};
bw->Write(SomeBinaryData);
bw->Flush();
bw->Close();
}
void BinaryReaderDemo()
{
Console::WriteLine("Reading C:\\nish.com");
FileStream* fs = new FileStream("C:\\nish.com",
FileMode::Open,FileAccess::Read);
BinaryReader* br = new BinaryReader(fs);
unsigned char c;
while(br->PeekChar() != -1)
{
c = br->ReadByte();
Console::Write("{0:X2} ",__box(c));
}
br->Close();
Console::WriteLine();
}
void StreamWriterDemo()
{
Console::WriteLine("Creating C:\\TempFile.txt");
StreamWriter *sw = new StreamWriter ("C:\\TempFile.txt");
sw->WriteLine("This is the first line");
sw->WriteLine("This is the second line");
sw->WriteLine("This is the third/last line");
sw->Close();
}
void StreamReaderDemo()
{
Console::WriteLine("Reading C:\\TempFile.txt");
Console::WriteLine("-----------------------");
StreamReader* sr = new StreamReader ("C:\\TempFile.txt");
String* s;
while(s = sr->ReadLine())
Console::WriteLine(s);
sr->Close();
}
void StringReaderDemo(String* s)
{
Console::WriteLine("Reading the string [{0}]",s);
StringReader* sr = new StringReader(s);
int c;
while((c = sr->Read()) != -1)
Console::Write("{0} ",__box(Convert::ToChar(c)));
Console::WriteLine();
sr->Close();
}
void pause()
{
Console::WriteLine("\r\nPress any key...\r\n");
_getch();
}
private:
void Show(String *p, Object* s)
{
Console::WriteLine("{0} : {1}",p->PadRight(20),s);
}
};
int _tmain(void)
{
FileStuffDemo* fstuff = new FileStuffDemo();
fstuff->FileInfoDemo();
fstuff->pause();
fstuff->DirectoryInfoDemo1();
fstuff->pause();
fstuff->DirectoryInfoDemo2();
fstuff->pause();
fstuff->BinaryWriterDemo();
fstuff->pause();
fstuff->BinaryReaderDemo();
fstuff->pause();
fstuff->StreamWriterDemo();
fstuff->pause();
fstuff->StreamReaderDemo();
fstuff->pause();
String* s = "Sometimes I wonder why!";
fstuff->StringReaderDemo(s);
fstuff->pause();
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.
Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.
Nish's Technology Blog :
voidnish.wordpress.com