LEC20081113 - Introduction to Computer Programming November...

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

View Full Document Right Arrow Icon
Introduction to Computer Programming November 13, 2008 CSC180 Fall 2008, University of Toronto
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 operations Stream: any source of input or any destination for output. A text file contains bytes, each byte corresponding to an ASCII character. A binary file also contains bytes, however each byte not necessarily representing an ASCII character. A file, text or binary, is just a sequence of bytes. UNIX does not differentiate between text files and binary files. They are stored in the same way. Write text files and binary files: CSC180 Fall 2008, University of Toronto 1
Background image of page 2
#include <stdio.h> int main() { int x; FILE *fp; x = 0xBA1170; if ((fp = fopen("data.bin", "wb")) == NULL) return 1; /* write x into a binary file data.bin */ fwrite(&x, sizeof(x), 1, fp); fclose(fp); if ((fp = fopen("data.txt", "w")) == NULL) return 1; /* write BA1170 to a text file data.txt */ fprintf(fp, "%c", ’B’); CSC180 Fall 2008, University of Toronto 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
fprintf(fp, "%c", ’A’); fprintf(fp, "%c", ’1’); fprintf(fp, "%c", ’1’); fprintf(fp, "%c", ’7’); fprintf(fp, "%c", ’0’); fclose(fp); return 0; } How is the hexadecimal number BA1170 stored in a text file and in a binary file: The ASCII text file: 4241 3131 3730 (6 bytes) The binary file: CSC180 Fall 2008, University of Toronto 3
Background image of page 4
7011 ba00 (4 bytes) In emacs, use the command M-x hexl-mode to examine the hexademical representation of a file. In terminal, use the command xxd . Example: [lanhui@remote ~]$ xxd test.o 0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000 .ELF. ........... 0000010: 0100 0300 0100 0000 0000 0000 0000 0000 ................ 0000020: 3401 0000 0000 0000 3400 0000 0000 2800 4....... 4..... (. 0000030: 0b00 0800 5589 e583 ec18 83e4 f0b8 0000 ....U. .......... 0000040: 0000 83c0 0f83 c00f c1e8 04c1 e004 29c4 .............. ). ... Open file, close file, seek file, tell the value of the file position indicator, and rewind file: CSC180 Fall 2008, University of Toronto 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
#include <stdio.h> FILE *fopen(const char *path, const char *mode); int fclose(FILE *fp); int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream); void rewind(FILE *stream); whence : #define SEEK_SET 0 /* Seek from beginning of file. */ #define SEEK_CUR 1 /* Seek from current position. */ #define SEEK_END 2 /* Seek from end of file. */ Mode strings for text files: CSC180 Fall 2008, University of Toronto 5
Background image of page 6
r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 25

LEC20081113 - Introduction to Computer Programming November...

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

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