Introduction
A file representing a sequence of bytes on the disk where group of related data is stored. There are many different types of files we can use. For example Documents, Microsoft word, digital pictues,digital music and digital videos ,it can even be think as a digital version of a real world things in which we can interact by using different applications and often viewed as a creating and editing files.
File stream class hierarchy
A stream is a flow of data, in object oriented programming; the streams are controlled using the classes. The type's if stream and of stream are stream classes in C++ designed to be connected to input or output files. File stream objects have all the member functions and manipulators possessed by the standard streams, cin and cout.
For Example:
File operation modes
In C, we can perform four major operations on the file which may be text or binary.
Ø Creating a new file
Ø Opening an existing file
Ø Closing a file
Ø Reading from or writing contents to a file
Creation and closing of files
Ø Opening a file for creation is performed using the library function in the stdio.h header file can be fopen()
ptr=fopen("fileopen","mode")
for example: fopen("E:\\C Program\\newprogram.txt")
Ø Closing of files is performed by using library function fclose()
fclose(fptr):\\fptr is the file pointer associated with file to be closed
Detecting end of the file
we can detect when the end of the file is reached by using the member function eof() which is a special function that returns nonzero when there is no data to read from a input file streamand zero otherwise, syntax is as follows :
inteof();
For Example:
ifstream fin ;
fin.open("master", ios::in | ios::binary);
while(!find.eof()) //as long as eof() is zero
{ //that is, the file's end is not reached
: //process the file
}
if(fin.eof()) //if non-zero
cout<< "End of file reached ! \n
Reading text from a file
We can read information from the file in our program where we use the stream extraction operator(>>) as we use the operator to input information from the keyboard only difference is that we use ifstream or fstream object replacing of cin object.
For example:
#include<iostream>
#include<fstream>
usingnamespacestd;
int main() {
ifstreammyReadFil;
myReadFil.open(
"text.txt");
char output[100];
if (myReadFil.is_open()) {
while (!myReadFil.eof()) {
myReadFil>> output;
cout<<output;
}
}
myReadFil.close();
return 0;
}
Writing text to the file
we can write text to the file by using the stream insertion operator(<<) same as we use in the operator to output information to the screen,the only difference is that we can use ofstream or fstream object replacing cin object
1. #include<fstream.h>
2.
3. void main
4. {
5. ofstream file;
6.
7. file.open("file.txt");//open a file
8.
9. file<<"Helllo file\n"<<75;//write to it
10.
11. file.close();//close it
12. }
Copying the contents of the file
To copy a content of the file, the following steps are to be followed
Ø Open source file in a read mode
Ø Open target file in a write mode
Ø Read source file character by character in a line
Example program is as follows:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstreamfs;
ofstreamft;
charch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (like files.txt) : ";
gets(fname1);
fs.open(fname1);
if(!fs)
{
cout<<"Error in opening source files..!!";
getch();
exit(1);
}
cout<<"Enter target file name with extension given (like filet.txt) : ";
gets(fname2);
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
cout<<"File copied is success.!!";
fs.close();
ft.close();
getch();
}
1.9Storing class object in a file
A storage class is defined as the scope variables with and/or function within a c++program, these specifier precede the type that they modify.
The following storage classes can be used as:
Ø auto:intvar;
Ø register:registerintvar;
Ø extern: extern int count;
Ø static
Accesing of contents
It is possible to retrieve contents of an object that was created in the main function from another class..Here is the example code:
#include"ClassA.h"
#include"ClassB.h"
int main()
{
ClassAobjectA;
return0;
ClassA.h
classClassA
{
public:
ClassA():_privateVar(100)
{};
~ClassA()
{};
//Getters
floatgetPrivateVarA(){ return _privateVarA; };
//Setters
voidsetPrivateVarA(float privateVarA){ _privateVarA = privateVarA; };
private:
//Just a value
float _privateVarA;
ClassB.h
classClassB
{
public:
ClassB():_privateVarB(50)
{ };
~ClassB()
{ };
//This is what i´m trying to achieve: AccesobjectA
// like this: objectA.getPrivateVarA(); or objectA.setPrivateVarA;
intgetPrivateVarB(){return _privateVarB;
private:
int _privateVarB;
};
Checking for errors
File verification is the process of using an algorithm for verification of the integrity of a computer file. It can be done by comparing two files bit-by-bit, but it requires two copies of the same file, and may have chances of missing systematic corruptions which might occur to both files. A more popular approach is to also use to store checksums (hashes) of files; this is also called as message digests.
It can be done by two methods
Integrity verification : File integrity can be compromised it is mostly referred to as the file becoming corrupted. A file can become corrupted by a variety of ways: in this we considering faulty storage media, errors in transmission, write errors during copying or moving, software bugs,
Authenticity verification: This is often required to verify that a file modification is not done in transmission or in storage by entrusted third parties, for example, to include malicious code such as viruses . To check the authenticity, a classical hash function is not only enough for as they are not designed to be in a collision resistant; it is computationally trivial for an attacker to cause deliberate hash collisions, meaning that a malicious change in the file is not detected with by a hash comparison..
How we help you? - C++ Assignment Help 24x7
We offer Working with files assignment help, Working with files assignment writing help, assessments writing service, Working with files tutors support, step by step solutions to Working with files problems, Working with files answers, C++ assignment experts help online. Our C++ assignment help service is most popular and browsed all over the world for each grade level.
There are key services in Working with files which are listed below:-
- C++ programming help
- Assignment Help
- Homework Help
- C++ Assessments Writing Service
- Solutions to problems
- Experts support 24x7
- Online tutoring
Why choose us - The first thing come in your mind that why choose us why not others what is special and different about us in comparison to other site. As we told you our team of expert, they are best in their field and we are always live to help you in your assignment which makes it special.
Key features of services are listed below:
- Confidentiality of student private information
- 100% unique and original solutions
- Step by step explanations of problems
- Minimum 4 minutes turnaround time - fast and reliable service
- Secure payment options
- On time delivery
- Unlimited clarification till you are done
- Guaranteed satisfaction
- Affordable price to cover maximum number of students in service
- Easy and powerful interface to track your order