• (2 Pages)
  • +
  • 1
  • 2

Basic C++ File I/O Rate Topic: ****- 6 Votes

#1 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Posted 29 December 2005 - 01:32 PM

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

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.

 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.

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
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
  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.

#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.

Is This A Good Question/Topic? 0
  • +

Replies To: Basic C++ File I/O

#2 jenniweni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 06

Post icon  Posted 23 August 2006 - 03:02 AM

If your file has say four columns of data, how do you get for example columms 2 to 4 outputted?
Was This Post Helpful? 0
  • +
  • -

#3 virendra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 06

Posted 15 September 2006 - 04:49 AM

[color=#6600CC]:rolleyes: 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:
Was This Post Helpful? 0
  • +
  • -

#4 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Posted 02 October 2006 - 03:02 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 virendra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 06

Post icon  Posted 04 October 2006 - 05:17 AM

:huh: 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:
virendra
Was This Post Helpful? 0
  • +
  • -

#6 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Posted 13 October 2006 - 03:51 PM

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.
Was This Post Helpful? 0
  • +
  • -

#7 DPR  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 20-December 06

Posted 23 December 2006 - 04:01 PM

Hmm...

Your way of reading files:
 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:

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:
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 23 December 2006 - 09:32 PM

Very True, DPR.
I was just about to say that :)
Was This Post Helpful? 0
  • +
  • -

#9 iry  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-September 07

Posted 27 October 2007 - 11:35 PM

v hv 2 close back d file, right?
but.... i didn c it~
Was This Post Helpful? 0
  • +
  • -

#10 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Posted 02 May 2008 - 04: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.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Posted 03 June 2008 - 06:38 AM

View Postpolymath, on 2 May, 2008 - 05:48 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#12 smita929  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-January 09

Posted 21 January 2009 - 06:06 AM

could you also tell how to append in a file...
Was This Post Helpful? 0
  • +
  • -

#13 Masterchief117.arbiter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 159
  • Joined: 27-March 09

Posted 02 April 2009 - 09:05 AM

can you tell me how to null fp

i know that it is the reason that my program only runs with admin rights
Was This Post Helpful? 0
  • +
  • -

#14 Masterchief117.arbiter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 159
  • Joined: 27-March 09

Posted 06 April 2009 - 08:59 AM

Can you please show me how to void file permissions
Was This Post Helpful? 0
  • +
  • -

#15 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 02 July 2009 - 07:22 AM

:D
I love your writing style!

If you run for president of USA I will vote for you!
:D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2