Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,789 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,798 people online right now. Registration is fast and FREE... Join Now!




FILE input and output( basic tutorials)

 
Reply to this topicStart new topic

> FILE input and output( basic tutorials), how to use FILES

Rating  4
bluesuus
Group Icon



post 29 Dec, 2005 - 12:32 PM
Post #1


FILE OPERATIONs

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
}

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


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


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")*/


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;
}


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;
}


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jenniweni
*



post 23 Aug, 2006 - 02:02 AM
Post #2
If your file has say four columns of data, how do you get for example columms 2 to 4 outputted?
Go to the top of the page
+Quote Post

virendra
*



post 15 Sep, 2006 - 03:49 AM
Post #3
[color=#6600CC]rolleyes.gif sir when u will submit the next tutorial regarding the file basic operation
im in waiting bcos i have lot of confusion regarding the file and streams, im doing some simple programming on c++ in file handlig
so will u provide any type of refence link on basi of that i can learn a lot in file till the ur next tutorial will available , bcos i mvery week in this section of programming & i want to become expert inc,& c++ . so pls guide me how can i devlop the efficiency of doing programming
in c &C++,
im computer engineer
virendra huh.gif
Go to the top of the page
+Quote Post

bluesuus
Group Icon



post 2 Oct, 2006 - 02:02 PM
Post #4
virendra......you see the most efficient way to learn programming is by reading alot of material concerning ur objective and then Practice alot.and also im going to continue the tutorial probably this week.And also u might want to check my program in the code Snippet section( its called CDBurn.dat ).It has alot of the basics of file.
Go to the top of the page
+Quote Post

virendra
*



post 4 Oct, 2006 - 04:17 AM
Post #5
huh.gif hey bluesis dear there is no CDBurn.dat in code snippet section i searche aal the keyword would u please provide me the exact location CDBurn.dat
thank lot for such precious reply. rolleyes.gif
virendra
Go to the top of the page
+Quote Post

bluesuus
Group Icon



post 13 Oct, 2006 - 02:51 PM
Post #6
sorry my mistake search 4 the keyword "Backup" in the code snippet section..The code uses all the things i explained in the 1st tutorial.
Go to the top of the page
+Quote Post

DPR
*



post 23 Dec, 2006 - 03:01 PM
Post #7
Hmm...

Your way of reading files:
CODE
int num;
in >> num;
while( num )
{
cout << num << endl;
in >> num;
}


is very clumsy. If the number in the file was a 0 it would exit the loop because 0 = FALSE. I think a much better way of reading a file until the end is this:

CODE
int num;

while(!in.eof())
{
    in >> num;
    cout << num << endl;
}

eof() is a standard fstream function that returns true if the cursor is at the end of the file (in this case, in).

Just to let you know. rolleyes.gif
Go to the top of the page
+Quote Post

born2c0de
Group Icon



post 23 Dec, 2006 - 08:32 PM
Post #8
Very True, DPR.
I was just about to say that smile.gif
Go to the top of the page
+Quote Post

iry
*



post 27 Oct, 2007 - 10:35 PM
Post #9
v hv 2 close back d file, right?
but.... i didn c it~
Go to the top of the page
+Quote Post

polymath
Group Icon



post 2 May, 2008 - 03:48 PM
Post #10
As a sugestion for the next part, you could cover how to create files and fill them with data for later use by the same program (ie saved games, but not that complex). Thats all.
Go to the top of the page
+Quote Post

KYA
Group Icon



post 3 Jun, 2008 - 05:38 AM
Post #11
QUOTE(polymath @ 2 May, 2008 - 05:48 PM) *

As a sugestion for the next part, you could cover how to create files and fill them with data for later use by the same program (ie saved games, but not that complex). Thats all.


Poly I have written a snippet for you (pending approval) on how to create files and write data to them for later retrieval.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/20/08 03:39PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month