FILE MANAGEMENT
File management
• Files which are on the secondary storage device are called physical
files.
• In order to process file through program, logical file must be created
on the RAM. This logical file has file data type.
• STREAM: File i/o objects as well as the objects cin and cout.
• Stream is a flow of characters or other kind of data.
• If the flow is into your program:= input stream
• If the flow is out of your program:= output stream
• FILE: the actural
• Standard streams: whenever a program starts execution, 3 streams
are opened automatically.
• stdin: standard input
• stdout: standard output
• stderr: standard error
• Normally, these streams refers to the console.
C++ file i/o classes and functions
• The header file fstream is required. It defines several classes including
ifstream, ofstream, and fstream.
• ifstream – can be used for file read/input operations
• ofstream – can be used for file writing/output operations
• fstream – can be used for both read/write c++ file i/o operations
• In file processing, files generally classified in to two as
• Text file: the content is treated as a sequence of characters and can be accessed
• Binary file : the content is treated as record sequence in a binary format. Binary
format refers to the actual format the data is going to be placed and processed
in the memory which is directly related to its data type.
Text file processing
File processing steps
• Declaring file variable identifier
• Opening the file
• Processing the file
• Closing the file when the process is completed.
Example : writing to a disk
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile; //declaring file variable identifier
myfile.open("example1.txt"); //opening the file
myfile<<"writing this to a file.\n"; //processing the file
myfile.close(); //closing the file
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile( "example.txt");
if(myfile.is_open())
{
myfile <<"This is a line. \n";
myfile << "This is another line.\n";
myfile.close();
}
else
cout<<"Unable to open file";
}
#include <iostream>
#include <fstream> Reading a
#include <string>
using namespace std; file
int main()
{
string line;
ifstream myfile ("example.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{ getline(myfile,line);
cout<<line<<endl; }
myfile.close();
}
else cout<<"Unable to open file";
}
END OF THE COURSE
Final exam contents
• Theory 20% [10 questions choose]
• Basic definition of the terms
• Select the correct output
• Code 30%
• Writing a code
• E.g. write a program that calculates the area of circle [A=πr2]
• E.g. pyramid stars using for loop.
• E.g. a program that display the student name and grade.
• Function calling
• Writing output
• For the given code write the output. Including pointer, array, structure …
Mid exam grading [20+2%]
• Part I: choose: Total 4 %
• 1-8: each 0.5%
• Part II: Total 18%
• 1: explanation + example = 2%
• 2: explanation + example = 2%
• 3: explanation and comparison = 2%
• 4: a+b =1 % c=1% d=1%
• 5: a= 1% b=1%
• 6: a+b =1% c=1% d=1%
• 7: algorithm + flowchart = 2%
• Bonus: comment on the first line plus correct code = 2%
D:\_DU\Y2 S1