Ladies and Gentlemen.We are in the presence of one of the most important computer feature used in wherever u can think of,homes,offices, companies,projects e.tc.This feature without it.computer i wont say wud be useless but it will become 50% less efficient. The name given to this outstanding feature is "FILE".
"FILes", this tutorial im writing is a file, whichs is just a bunch of text im writing.A collection of text whether garbage or not is called a file.OKay, lets say you have created a file and saved it as "myFile.dat".How can we manipulate it ( e.g extract a part of it, delete some part of it, encrypt it, e.tc so many things)?With the help of computer prrograms like c/c++, we can easily manipulate tha given file(in this case, myFile.dat).Enough of that buch of text( whats a bunch of text called..... a File ).Im goint to indroduce how to open and manipulate files using c / c++.so put your seatbelts on cuz the Traffic light is Green.
C++/C I/O operations
C++ commands to open files
1) ifstream( class to open file for reading)
2) ofstremn( class to open file for writing )
3) all of this classes extend from their main super class or father called "fstream".
As i said classes, just as you create e.g your "Quadilateral class"(which is more of like the fstream class as in this example )
CODE
class Quadilateral {
double getArea();//example
}
double getArea();//example
}
Ractangle, square are all Quadilaterals,likewise ifstream and ofstream are all fstream(confusing?i knw, read a tutorial on classes) any Quadilateral has to know its area.in this example our Rectangle and shape are more of like our "ifstream" and ofstream" .get a tutorial or request one to be written.
Okay lets get back to businees.
we want to open our littile file "myFile.dat".How do we do it in c++?its simple,
1)we have to create an object of type ifstream(remember before you can use a class u have to instantiate it.This is exactly what we are doing);
CODE
ifstream in; */ you can put anything apart from "in" its ur choice but put something meaningfull, sumthing that has to do with reading data*/
2) we now use one of the methods of the class ifstream( which it may or may have not inherited from its father(fstream) ).I havnt had much time to check how fstreeam was created but i think its more of an abstract class in which all its subclasses can implement its methods.The designer of the class created many method of which we have "open", "close" e.t.c. lets see an example, of how to open out little file "myFile.dat" for reading.
CODE
in.open("myFile.dat"); */ remeber the object "in" we created from the class ifstream */
in.close(); //closes the file
in.close(); //closes the file
The same thing happens when you re opening a file for writing just foloow the steps but this time we are using the ofstream class.
CODE
ofstream out; //" out" can be any name but meaningful
out.open( "output.dat" ); * / open a file for wrintg and naming it "output.dat" */
out.close(); // closes the file
out.open( "output.dat" ); * / open a file for wrintg and naming it "output.dat" */
out.close(); // closes the file
PHEW, That was a hell lot of "FILE"(if u knw what i mean,bunch of text) for simple statements.
okay so know we have opened our file "myFile.dat" for reading, automatically the cursor is at the begining of the file.note, we dont knw what "myFile.dat" contains but we knw it could be numbers, bunch of words, mixture of both, e.t.c.Let stick with numbers because its somewhat the same idea you will c .
okay we want to read a number ofrom the file.here is code
CODE
int num; //variable to store number we read from file
in >> num; */ remeber how to read from keyboard "cin >> num".this is exactly it but this time we are not reading from the keyboard but from our file we opened using our object("in")*/
in >> num; */ remeber how to read from keyboard "cin >> num".this is exactly it but this time we are not reading from the keyboard but from our file we opened using our object("in")*/
if our file consisted of strings we can read each word by substituting the "int" with "string". Back to numbers, u read your first number and store it in variable num, u can print it using "cout << num;"
OKay hold on now, remeber we said we dont knw how many data our file contains, so obviously it might have contained only one data which we have read and stored in number, or it could have many more data in the file.How can we know?
Okay, you...The guy at the front row typing this tutorial answer that question.LOL
so we want to read the file till the end of file because we do not how many numbers it contains or the size of the file. so to read here is the code, we use a loop, i prefer while loop cuz u will c, its easier.modifying our code we knw hav
CODE
int num;
in >> num;
while( num )
{
cout << num << endl;
in >> num;
}
in >> num;
while( num )
{
cout << num << endl;
in >> num;
}
what the code does, is after declaring the variable num, it reads the first num from the file( remeber the cursor is automatically at the begining of the file). it then checks in the wile loop whether the operation "in >> num" was successfull. if successfull, it enters the loop.which then prints the value, and then reads another num, it goes back to the loop to test whether it is successfull.After all the data have been read the condition will be false and you are out the loop, and have printed all the data in the file. This is the complete code.
CODE
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in;
ofstream out;
int num; // remeber it can be string
in.open("myFle.dat");
in.open("output.dat");
in >> num;
while ( num )
{
cout << num << endl;
in >> num;
}
return 0;
}
#include <fstream>
using namespace std;
int main()
{
ifstream in;
ofstream out;
int num; // remeber it can be string
in.open("myFle.dat");
in.open("output.dat");
in >> num;
while ( num )
{
cout << num << endl;
in >> num;
}
return 0;
}
Thats it for the basics, ill be continuing this tutorial.its very hard to get everything, so dats why im stopping here, to allow room for questions. so u should keep on checking for the update.Take care guys..Ask questions, thats the best way to learn.
