Upload
ashley-mosley
View
221
Download
0
Embed Size (px)
Citation preview
Lecture 09: Files
http://oldcomputers.net/floppydisks.html
Contents
I. Overview
II.Text / /*Binary*/ files in C (FILE * and functions)– Writing– Reading
III.Text / /*Binary*/ files in C++ (fstream)– Writing– Reading
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– ...
Storage: Magnetic
● Variants:– Tape Drives– Floppy drives– “normal” hard drives
● A magnetic media + read/write head
http://www.wps.com/NOVA4/pitchas.html
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
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
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
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*);
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);
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;}
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)
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();
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;}
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.