Solved

Creating a text file containing no control characters?

Posted on 2004-08-02
7
142 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:judico
  • 3
  • 2
7 Comments
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
HANDLE      hFile = CreateFile(m_strFileName, GENERIC_READ, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_HIDDEN, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
      OutputDebugString(_T("\nError creating file"));
            
}      
else
{      
                TCHAR      szLine[] = _T("Some text here");
      DWORD      dwBytes;
                WriteFile(hFile, szLine, sizeof(szLine), &dwBytes, 0);
      CloseHandle(hFile);
{
0
 

Author Comment

by:judico
Comment Utility
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.
0
 

Author Comment

by:judico
Comment Utility
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" );   ?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Expert Comment

by:nonubik
Comment Utility
StreamWriter* pwriter = new StreamWriter( S"c:\\My_Directory\\KBTest.txt" );  
0
 

Author Comment

by:judico
Comment Utility
This is what I needed:

//This demonstrates how to READ a text file:
      private: System::Void button1_Click(System::Object *  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::DialogResult 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(reader->ReadLine());
      }  
      while(reader->Peek() != -1);
}    
catch(FileNotFoundException *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::Object *  sender, System::EventArgs *  e)
             {

SaveFileDialog *fileChooser = new SaveFileDialog();
Windows::Forms::DialogResult result = fileChooser->ShowDialog();

String *fileName;

fileChooser->CheckFileExists = 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.

0
 

Accepted Solution

by:
CetusMOD earned 0 total points
Comment Utility
Closed, 50 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Doc'in system (example?) BA 7 78
fizzArray  challenge 1 47
matchUp  challenge 6 48
how to split multiple lines delimiter : 8 53
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now