11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

164 • Chapter 4: Input and Outputany file operation. When you use descriptors, however, you must explicitlyopen the file first to create the descriptor.The two commands for opening files are fopen and open. The fopencommand opens buffered (STREAM) files, whereas, the open commandopens unbuffered (RAW) files.Use the fopen command as follows.fopen( fileName, accessMode, fileType )The fileName specifies the name of the file to open. This name is specifiedas a string, and follows the conventions that the underlying operatingsystem uses. The accessMode must be one of READ, WRITE, or APPEND,indicating whether you should initially open the file for reading, writing,or appending. The optional fileType is either TEXT or BINARY.If you try to open the file for reading and it does not exist, fopengenerates an error.If you try to open the file for writing and it does not exist, Maplecreates it. If it does exist and you specify WRITE, Maple truncates the fileto zero length; if you specify APPEND, subsequent calls to commands thatwrite to the file append to it.Call the open command as follows.open( fileName, accessMode )The arguments to open are the same as those to fopen, except that youcannot specify a fileType (TEXT or BINARY). Maple opens an unbufferedfile with type BINARY.Both fopen and open return a file descriptor. Use this descriptor torefer to the file for subsequent operations. You can still use the filename.When you have finished with a file, instruct Maple to close it. This ensuresthat Maple writes all information to the disk. It also frees resourcesof the underlying operating system, which often imposes a limit on thenumber of files that you can open simultaneously.Close files by using the fclose or close commands. These two commandsare equivalent. You can call them as follows.fclose( fileIdentifier )close( fileIdentifier )The fileIdentifier is the name or descriptor of the file to close. Once youclose a file, any descriptors referring to the file are no longer valid.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!