1 / 10

File I/O

File I/O. I/O Flags. Flags are passed to give some information about how the file is to be used. Read only file – flag=0x0 Write only file – flag=0x1 Read and write – flag=0x2 Also tell other info by ORing above with Create – flag=0x100 Truncate – flag=0x200 Append – flag=0x8

avidan
Download Presentation

File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File I/O

  2. I/O Flags • Flags are passed to give some information about how the file is to be used. • Read only file – flag=0x0 • Write only file – flag=0x1 • Read and write – flag=0x2 • Also tell other info by ORing above with • Create – flag=0x100 • Truncate – flag=0x200 • Append – flag=0x8 • And tell the type by ORing above with • Text – 0x4000 • Binary – 0x8000

  3. File Descriptor • This is a value (held in 4 bytes = 1 word) that uniquely identifies the file. • It is provided when the file is opened. • This is used when accessing the file to identify the file. • The open uses the file name and the system assigns a file descriptor value to be used by the other file I/O commands

  4. File Open • Use syscall with $v0 code 13 • $a0 has address of string containing the filename (zero/null terminated with no LF). • $a1 flags • $a2 permissions • $v0 file descriptor for future use (-1 for error)

  5. File Read • Use syscall code 14 • $a0 has the file descriptor (from the open) • $a1 has the address of the buffer of where to put the stuff read in (character string) • $a2 has the size of the buffer. • $v0 returns the count of how much data was read in (-1 for error, 0 for EOF)

  6. File Write • Uses syscall code 15 • $a0 has the file descriptor • $a1 has the address of the buffer to write out (character string) • $a2 has the size of the buffer to write out • $v0 returns the amount actually written (should be the same as $a2 unless there was a problem)

  7. File Close • Uses syscall code 16 • $a0 has the file descriptor • THAT’S IT

  8. Review of Read String • Uses syscall code 8 • $a0 has the address of where to put the string (buffer) • $a1 has the size of the buffer • The input is read up the return character and a LF is put at the end of the string along with the null character (after the LF). • Some applications don’t want the LF so it needs to be removed.

  9. ASCII • All the file I/O commands use the address of a buffer. • This I/O is all done without conversion to ASCII characters. • You can write out binary numbers by placing them in the buffer, however these values will not be displayable on the screen (much like executable files). • To write numbers to a file that you can see, you must convert to ASCII • However, you can write numbers to a file in binary and have another program read them (not a human).

  10. Character I/O • Syscall code 11 is print a character • The character is in the lower byte of $a0 • Syscall 12 is read a character • $v0 contains the character in the lower byte • Read a character does not wait for the return

More Related