Top Banner
Lecture 09: Files http://oldcomputers.net/ floppydisks.html
14

Lecture 09: Files l.

Dec 28, 2015

Download

Documents

Ashley Mosley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 09: Files  l.

Lecture 09: Files

http://oldcomputers.net/floppydisks.html

Page 2: Lecture 09: Files  l.

Contents

I. Overview

II.Text / /*Binary*/ files in C (FILE * and functions)– Writing– Reading

III.Text / /*Binary*/ files in C++ (fstream)– Writing– Reading

Page 3: Lecture 09: Files  l.

Part I: Overview

● Files are basically a bunch of numbers– Just like RAM– Encoded in binary

● Organized into bytes, kilobytes, …● Need to convert from real life => numbers

● Can be stored in many different ways– ...

Page 4: Lecture 09: Files  l.

Storage: Magnetic

● Variants:– Tape Drives– Floppy drives– “normal” hard drives

● A magnetic media + read/write head

http://www.wps.com/NOVA4/pitchas.html

Page 5: Lecture 09: Files  l.

Storage: Flash memory

● Variants– Flash-drives– Solid-state Hard “disks”– SD cards

● Principle– Each bit is a transistor

● Think of it as a “bottle” capable of holding charge.

– A circuit connected to the transistor can either turn it on or off.– Does not require power to keep the charge– Have a finite number of read/writes that can be applied to it.

http://en.wikipedia.org/wiki/Flash_memory

Page 6: Lecture 09: Files  l.

Storage: Optical Media

● Variants– CD– DVD

● Principle:– Pits / Grooves on media– Laser detects distance

http://threesixtyltd.en.made-in-china.com/product/foyxqQKjnJkF/China-DVD-Laser-Head-KHM-313AAA-KHM-313AHC-KHM-280AAA-.html

Page 7: Lecture 09: Files  l.

Connection to a program

● [Low-level hardware]● [Device Controller]● [Device Driver]● [Operating System]● [A File Object]

– Maintains current read and write position– Has mechanisms to stream bytes to / from– The connection must be opened / closed by programmer

Page 8: Lecture 09: Files  l.

Part II: C File-access● FILE structure● Functions

FILE * fopen(char * name, char * mode);

void fclose(FILE *);

int fprintf(FILE *, char * formatStr, …);

int fscanf(FILE *, char * formatStr, …);

char * fgets(char * str, int num, FILE *);

long int ftell(FILE *);

//int feof(FILE *);

//int fseek(FILE *, long int offset, int origin);● origin can be one of SEEK_SET, SEEK_CUR, SEEK_END

//int fread(void *, size_t size, size_t count, FILE *);

//int fwrite(void *, size_t size, size_t count, FILE*);

Page 9: Lecture 09: Files  l.

Example (text files)

#include <stdio.h>#include <string.h>using namespace std;

int main(){FILE * fp;int bytes_written, i1, i2, i3;float f;char str[256];

// Open a file for writing and dump some data to it, then close it fp = fopen("test.txt", "w"); fprintf(fp, "%d:%s %f\n%s\n%d===%d\n", 57, "short-string", 3.14f, "A really long string", 15, 99); bytes_written = ftell(fp); printf("%d bytes written to 'test.txt'\n\n", bytes_written); fclose(fp);

Page 10: Lecture 09: Files  l.

Example (text files), cont.

// Re-open the file and attempt to read data out of it fp = fopen("test.txt", "r"); fscanf(fp, "%d:", &i1); fscanf(fp, "%s", str); fscanf(fp, "%f", &f); printf("Test: first int=%d, first string=%s, first float=%f\n",

i1, str, f); fgets(str, 255, fp); // To consume the newline at the end of the line. fgets(str, 255, fp); str[strlen(str)-1] = 0; // To get rid of new-line fscanf(fp, "%d===%d", &i2, &i3); printf("Test: second string=%s, second int=%d, third int=%d\n", str, i2, i3);

fclose(fp); return 0;}

Page 11: Lecture 09: Files  l.

Part III: C++ File-access

● fstream object (similar to cin / cout)● Methods

open(char * fname, ios_base::openmode);

operator <<

operator >>

close()

getline(char * s, streamsize n)

streampos tellp()

//bool eof()

//streampos tellg()

//read(char * s, streamsize n)

//write(char * s, streamsize n)

Page 12: Lecture 09: Files  l.

Example (text)

#include <fstream>#include <iostream>#include <string.h>using namespace std;

int main(){fstream fp;int bytes_written, i1, i2, i3;float f;char str[256];

fp.open("test.txt", ios::out); fp << 57 << " " << "short-string" << " " << 3.14 << endl; fp << "A really long string" << endl << 15 << "===" << 99 << endl; bytes_written = fp.tellp(); // tellg is the read-position cout << bytes_written << " bytes written to 'test.txt'\n"; fp.close();

Page 13: Lecture 09: Files  l.

C++ Example (text)

fp.open("test.txt", ios::in); fp >> i1; fp.ignore(1); // Ignore the ':' fp >> str >> f; cout << "Test: first int=" << i1 << ", first string=" << str << ", first float=" << f << endl; fp.getline(str, 255); // To consume the newline at the end of the line fp.getline(str, 255); //str[strlen(str)-1] = 0; // Not necessary to get rid of new-line fp >> i2; fp.ignore(3); // Ignore the "===" fp >> i3; cout << "Test: second string=" << str << ", second int=" << i2 << ", third int=" << i3 << endl; fp.close();

return 0;}

Page 14: Lecture 09: Files  l.

Last Lecture!

● You're done listening to me!● Now start studying for the final ;-P● Thanks for a very fun semester – you've ALL been

a pleasure to work with.