judico
asked on
Creating a text file containing no control characters?
I need to create a text file in VC++ .NET containing plain numbers with no control characters which can be read with, say, the Notepad (the way text (not binary) files are created in, say, VB.NET). It appears that all examples given in a book such as Deitel’s “Visual C++ .NET. How to Program” create files which contain control characters and encrypt the data. Is that it? Isn’t there a way to create plain and simple text files in VC++ .NET containing no additional characters that can be viewed by common text editors?
ASKER
I am having problems implementing above code in managed environment (when trying to use forms). Also, what is the corresponding code for reading the created text file? I would appreciate it very much if you could post a working example using forms in VC++ .NET. Thank you in advance.
ASKER
I found a link that seems to solve the problem:
http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;307398&product=vcNET#1
There were several typos to be corrected, otherwise it works fine. Now the problems with OpenFileDialog and SaveFileDialog remain to be solved. To start, how can one change the directory in which the file is written in the code:
StreamWriter* pwriter = new StreamWriter( S"\\KBTest.txt" ); ?
http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;307398&product=vcNET#1
There were several typos to be corrected, otherwise it works fine. Now the problems with OpenFileDialog and SaveFileDialog remain to be solved. To start, how can one change the directory in which the file is written in the code:
StreamWriter* pwriter = new StreamWriter( S"\\KBTest.txt" ); ?
StreamWriter* pwriter = new StreamWriter( S"c:\\My_Directory\\KBTest .txt" );
ASKER
This is what I needed:
//This demonstrates how to READ a text file:
private: System::Void button1_Click(System::Obje ct * sender, System::EventArgs * e)
{
// Use try...catch to deal with a 0 byte file or a non-existent file.
listBox1->Items->Clear();
try
{
OpenFileDialog *fileChooser = new OpenFileDialog();
Windows::Forms::DialogResu lt result = fileChooser->ShowDialog();
// name of file containing data
String *fileName;
// exit event handler if user clicked Cancel
if ( result == DialogResult::Cancel )
return;
// get specified file name
fileName = fileChooser->FileName;
// show error if user specifies invalid data
if ( ( fileName->Equals( S"" ) ) )
MessageBox::Show( S"Invalid File Name", S"Error",
MessageBoxButtons::OK, MessageBoxIcon::Error );
else {
String* textFile = String::Concat( fileName );
}
StreamReader *reader = new StreamReader(fileName);
do
{
listBox1->Items->Add(reade r->ReadLin e());
}
while(reader->Peek() != -1);
}
catch(FileNotFoundExceptio n *ex)
{
listBox1->Items->Add(ex);
}
catch (System::Exception *e)
{
listBox1->Items->Add(e);
}
}
// This demonstrates how to create and to WRITE to a text file.
private: System::Void button2_Click(System::Obje ct * sender, System::EventArgs * e)
{
SaveFileDialog *fileChooser = new SaveFileDialog();
Windows::Forms::DialogResu lt result = fileChooser->ShowDialog();
String *fileName;
fileChooser->CheckFileExis ts = false;
if ( result == DialogResult::Cancel )
return;
fileName = fileChooser->FileName;
if ( ( fileName->Equals( S"" ) ) )
MessageBox::Show( S"Invalid File Name", S"Error",
MessageBoxButtons::OK, MessageBoxIcon::Error );
else {
StreamWriter* pwriter = new StreamWriter( fileName );
pwriter->WriteLine( S"The file was created by using the StreamWriter class." );
pwriter->Close();
listBox1->Items->Clear();
String *filew = new String(S"File written to C:\efi\KBTest.txt");
listBox1->Items->Add(filew );
}
Now, the next step is to find out how to place the two columns of numbers in my text file in two separate arrays which I want to use later in the program.
//This demonstrates how to READ a text file:
private: System::Void button1_Click(System::Obje
{
// Use try...catch to deal with a 0 byte file or a non-existent file.
listBox1->Items->Clear();
try
{
OpenFileDialog *fileChooser = new OpenFileDialog();
Windows::Forms::DialogResu
// name of file containing data
String *fileName;
// exit event handler if user clicked Cancel
if ( result == DialogResult::Cancel )
return;
// get specified file name
fileName = fileChooser->FileName;
// show error if user specifies invalid data
if ( ( fileName->Equals( S"" ) ) )
MessageBox::Show( S"Invalid File Name", S"Error",
MessageBoxButtons::OK, MessageBoxIcon::Error );
else {
String* textFile = String::Concat( fileName );
}
StreamReader *reader = new StreamReader(fileName);
do
{
listBox1->Items->Add(reade
}
while(reader->Peek() != -1);
}
catch(FileNotFoundExceptio
{
listBox1->Items->Add(ex);
}
catch (System::Exception *e)
{
listBox1->Items->Add(e);
}
}
// This demonstrates how to create and to WRITE to a text file.
private: System::Void button2_Click(System::Obje
{
SaveFileDialog *fileChooser = new SaveFileDialog();
Windows::Forms::DialogResu
String *fileName;
fileChooser->CheckFileExis
if ( result == DialogResult::Cancel )
return;
fileName = fileChooser->FileName;
if ( ( fileName->Equals( S"" ) ) )
MessageBox::Show( S"Invalid File Name", S"Error",
MessageBoxButtons::OK, MessageBoxIcon::Error );
else {
StreamWriter* pwriter = new StreamWriter( fileName );
pwriter->WriteLine( S"The file was created by using the StreamWriter class." );
pwriter->Close();
listBox1->Items->Clear();
String *filew = new String(S"File written to C:\efi\KBTest.txt");
listBox1->Items->Add(filew
}
Now, the next step is to find out how to place the two columns of numbers in my text file in two separate arrays which I want to use later in the program.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (hFile == INVALID_HANDLE_VALUE)
{
OutputDebugString(_T("\nEr
}
else
{
TCHAR szLine[] = _T("Some text here");
DWORD dwBytes;
WriteFile(hFile, szLine, sizeof(szLine), &dwBytes, 0);
CloseHandle(hFile);
{