12_f09_FileIO - Windows File I/O Files Collections of related data stored on external storage media and assigned names so that they can be accessed

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Windows File I/O Files • Collections of related data stored on external storage media and assigned names so that they can be accessed later – Entire collection is a file – A file is made up of records • One record for each entity stored in the file • Each record broken down into fields (data elements) Last Name First Name Phone --------------------------------------------- Smith John 777-1111 Jones Mary 777-2222 Records Fields
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File I/O Under Win32 API and MFC • Use Standard C Library File functions FILE *fp; // a file pointer fp = fopen("filename_string", "mode_string"); //Open file and retrieve a pointer to it fread (buffer, size, number, fp); // Read number of size elements into address pointed to by buffer fwrite (buffer, size, number, fp); // Write number of size elements from buffer to file fseek(fp, offset, origin); // Go offset bytes in open file, measured from origin cur_posn = ftell(fp); // Retrieve current byte position in open file fclose(fp); // Close file pointed to by fp – To be able to use these, we must #include <stdio.h> • For details see: – http://www.cs.binghamton.edu/~reckert/360/class12.htm .NET File Class to Read/Write Files Fil es • File class in System.IO namespace • Static methods ReadAllText( ) and WriteAllText( ) – File.ReadAllText(string FilePath); – File.FileWriteAllText(string FilePath, string contents); – Can also use ReadAllBytes(…) and WriteAllBytes(…) for binary files • Examples: FileSimpleWrite and FileSimpleRead – Use a textbox for user to enter file name, a multiline text box for the contents of the file to be written or read and a button to initiate the reading/writing
Background image of page 2
Files and Streams in .NET • .NET Framework handles data files using Streams – When a file is opened for reading or writing it becomes a stream – Stream: an object that transfer a series of bytes from one location to another – Read and write operations can be performed on a stream – Streams can be more than just open disk files • Network Streams: Data moving over a network connection is a stream • Memory Streams: Memory to memory transfers • Most .NET File and Stream I/O support is implemented in System.IO namespace • Any file-handling project should include the statement: using System.IO; FileStream File I/O Class • FileStream – Most basic File I/O class: like C file pointer – Use to: open, read from, write to, and close files – To open or create a file, create an object of type FileStream – Some FileStream constuctors: FileStream(strFileName, Filemode); • Some Filemode properties: Create, Open, Append FileStream(strFileName, Filemode, FileAccess); • Some FileAccess properties: Read, Write, ReadWrite – Some FileStream Methods: int ReadByte(); int Read(byte[] abyBuffer, int iBufferOffset, int iCount); void WriteByte (byte byteValue); void Write(byte[] abyBuffer, int iBufferOffset, int iCount); long Seek(long offset, SeekOrigin origin);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problems with FileStream • C# casting is not as flexible as C casting
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2009 for the course ER CS 360 taught by Professor Reckert during the Spring '09 term at Istanbul Technical University.

Page1 / 13

12_f09_FileIO - Windows File I/O Files Collections of related data stored on external storage media and assigned names so that they can be accessed

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online