Download presentation
Presentation is loading. Please wait.
Published byEmma Cameron Modified over 9 years ago
1
Lecture 14 Arguments, Classes and Files
2
Arguments
3
Classes (.h – header files)
4
Classes (.cpp – Source files)
5
Classes A function in a class that does not have the classes scope in the implementation is out of scope and produces a linker error. A function in scope is defined using :: (){…} A class that uses another class which also makes use of that class can cause issues with cyclical dependency (a linker error) One can avoid this problem with strict separation of implementation (definition) and declaration. This is made possible due to Forward Declarations. It is generally good practice to use classes as pointers E.g. Car* mCar = new Car();
6
Files Files are used to A file can be written or read as plain text or binary Binary files are based on reading and writing bytes http://www.eecs.umich.edu/courses/eecs380/HANDOUTS/cpp BinaryFileIO-2.html You don’t need to know how to read/write binary files
7
#include #include #include class Student { private: int number; char name[50]; float gpa; public: Student(int n, const char *s, float g); void save(ofstream& of); void load(ifstream& inf); };
8
void Student::save(ofstream& of) { of.write(&number, sizeof(number)); of.write(name, sizeof(name)); of.write(&gpa, sizeof(gpa)); } void Student::load(ifstream& inf) { inf.read(&number, sizeof(number)); inf.read(name, sizeof(name)); inf.read(&gpa, sizeof(gpa)); }
9
main() { Student me(11321, "Myself", 4.3); ofstream myfile; myfile.open("silly.dat", ios::binary | ios::out); me.save(myfile); myfile.close(); return(0); }
10
ReadingFiles std::getline(input stream (istream&), string&) std::getline(input stream (istream&), string&, char) //delimiter ifstream.open(“file name”, mode) Modes belong to the ios namespace ios::in //open file for reading ios::open //open file for writing ios::binary //binary expected instead of text ios::ate //output position at end of file ios::app //append to file ios::trunc //truncate discard previous file information
11
Modes cont. Modes can be combined using the bitwise OR | e.g. ios::in | ios::app OR ios::in | ios::app | ios::binary Modes are set as default for file objects ofstream – ios::out ifstream – ios::in fstream – ios::in | ios::out close should be called when done with using the file object
12
File state flags bad() – returns true of an operation fails such as writing to a device with not enough space fail() – returns bad() but also returns true if a character is extracted instead of an expected integer from a file eof() – returns true for reading a file when the file has reached the end good() – Checks more state flags than bad(), mostly returns false
13
Exercise Create a file loading and writing program which takes a two command line arguments. The first determines whether a file should be read, the second determines the file to be read or written to. Use cin to input data to the file and use cout to output data read from the file.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.