OBJECT ORIENTED PROGRAMMING CIS 3100

Files are required to save our data for future use as

Info icon This preview shows pages 26–31. Sign up to view the full content.

View Full Document Right Arrow Icon
Files are required to save our data for future use, as Ram is not able to hold our data permanently. The Language like C/C++ treat every thing as a file , these languages treat keyboard , mouse, printer, Hard disk , Floppy disk and all other hardware as a file. The Basic operation on text/binary files are : Reading/writing ,reading and manipulation of data stored on these files. Both types of files needs to be open and close. How to open File Using member function Open( ) Using Constructor Syntax Filestream object; Object.open(“filename”,mode); Example ifstream fin; fin.open(“abc.txt”) Syntax Filestream object(“filename”,mode); Example ifstream fin(“abc.txt”); NOTE: (a) Mode are optional and given at the end . (a) Filename must follow the convention of 8.3 and it’s extension can be anyone How to close file Files Program Files Data Files Text Files Binary Files All types of files can be closed using close( ) member function Syntax fileobject.close( ); Example
Image of page 26

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Objective : To insert some data on a text file Program Program ABC.txt file contents #include<fstream> using namespace std; int main() { ofstream fout; fout.open("abc.txt"); fout<<"This is my first program in file handling"; fout<<"\n Hello again"; fout.close(); return 0; } This is my first program in file handling Hello again Reading data from a Text File Program file SCR Abc.txt Program Screen Keyboard
Image of page 27
#include<fstream> #include<iostream> #include<conio.h> using namespace std; int main() { ifstream fin; char str[80]; fin.open("abc.txt"); fin>>str; // read only first //string from file cout<<"\n From File :"<<str; // as //spaces is treated as termination point getch(); return 0; } NOTE : To overcome this problem use fin.getline(str,79); Detecting END OF FILE Using EOF( ) member function Using filestream object ABC.TXT
Image of page 28

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Syntax Filestream_object.eof( ); Example #include<iostream> #include<fstream> #include<conio.h> using namespace std; int main() { char ch; ifstream fin; fin.open("abc.txt"); while(!fin.eof()) // using eof() // function { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; } Example // detectting end of file #include<iostream> #include<fstream> #include<conio.h> using namespace std; int main() { char ch; ifstream fin; fin.open("abc.txt"); while(fin) // file object { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; } Example : To read the contents of a text file and display them on the screen . Program ( using getline member function) Program ( using get( ) member function) #include<fstream> #include<conio.h> #include<iostream> using namespace std; int main() { char str[100]; ifstream fin; fin.open("c:\\abc.txt"); while(!fin.eof()) { fin.getline(str,99); cout<<str; } fin.close(); getch(); #include<fstream> #include<conio.h> #include<iostream> using namespace std; int main() { char ch; ifstream fin; fin.open("file6.cpp"); while(!fin.eof()) { fin.get(ch); cout<<ch; } fin.close(); getch();
Image of page 29
return 0; } return 0; } 13. Explain Namespaces with examples. One of C++'s less heralded additions is addition of namespaces , which can be used to structure a program into "logical units". A namespace functions in the same way that a company division might function -- inside a namespace you include all functions appropriate for fulfilling a certain goal. For
Image of page 30

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 31
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern