11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

160 • Chapter 4: Input and Outputdecimal integer and a real number). It either returns a list of the resultingvalues or the integer 0 to indicate that it has reached the end of the file.The first time you call fscanf with a given file name, Maple prepares(opens) the file for reading. If it does not exist, Maple generates an error.The second line of the loop checks if fscanf returned 0 to indicate theend of the file, and breaks the loop if it has. Otherwise, Maple appends thepair of numbers to the list of pairs in A. (The syntax A := [op(A),xy]tells Maple to assign to A a list consisting of the existing elements of A,and the new element xy.)One Command for Opening, Reading, and Closing a File As when youwrite to a file, you can read from a file more easily by using the readdatacommand.> A := readdata("myfile2", [integer,float]);A := [[0, 0.], [1, 0.8427007929], [2, 0.995322265],[3, 0.9999779095], [4, 0.9999999846],[5, 1.000000000]]The readdata command performs all the operations of opening the file,reading the data, parsing the specified format (an integer and a floatingpointnumber), and then closing the file. However, readdata does notprovide the precise parsing control that you may need in some cases. Forthis, use fscanf directly.The next section expands on the basic concepts of the Maple I/Olibrary.4.2 File Types and ModesMost of the Maple I/O library commands operate on files. In this chapter,the term file is not limited to a disk file. It can include the default outputstream to a terminal or worksheet output region. Almost any operationthat you can perform on a real file you can perform on a data outputstream to the terminal or worksheet.Buffered Files versus Unbuffered FilesThe Maple I/O library can use two file types: buffered (STREAM) andunbuffered (RAW). Maple uses these files similarly.

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

Saved successfully!

Ooh no, something went wrong!