Menu Close

Working with text file

 Working with text file

1. namespace

The namespace System::IO is used to work with file reading and writing

 2. Save text string into a file

1). Create a StreamWriter

StreamWriter^ outfile = gcnew StreamWriter(“TextFilePathAndName.txt”);
2).  Write text to “TextFilePathAndName.txt”

outfile->Write(“Text to write”);

For other write method please refer to StreamWriter

3).  Close the StreamWriter

outfile->Close();

 For save binary data into a file, please refer to here.

3. Read text from a file

1). Check the file exists

File::Exists(“TextFilePathAndName.txt”);

2). If the file exists, open the file

StreamReader^  sr = gcnew StreamReader((“TextFilePathAndName.txt”);

3). Read text from the file

a). Read a line

String^ line =  sr->ReadLine();

b). For other read method please refer to StreamReader methods

4).  Close the file

 sr->Close();

 For read a birnary data from file, please refer to here.

2 comments on “Working with text file

  1. Pingback: Working with binary file | Code References

  2. cR1ibH on said:

    283651 886959Hi there, just became alert to your blog via Google, and discovered that it

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

18,577 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>