0% found this document useful (0 votes)
25 views4 pages

Sample Program

The document contains 5 C++ programs that demonstrate opening, writing, reading, and displaying information from files. Program 1 opens and writes a simple text string to a file. Program 2 opens a file and checks if it was opened successfully. Program 3 writes user-entered student data to a file in a loop. Program 4 reads student data from a file and displays it. Program 5 gets user input to display different types of items (harmful, helpful, plain) stored in an external "objects.txt" file.

Uploaded by

boiroy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Sample Program

The document contains 5 C++ programs that demonstrate opening, writing, reading, and displaying information from files. Program 1 opens and writes a simple text string to a file. Program 2 opens a file and checks if it was opened successfully. Program 3 writes user-entered student data to a file in a loop. Program 4 reads student data from a file and displays it. Program 5 gets user input to display different types of items (harmful, helpful, plain) stored in an external "objects.txt" file.

Uploaded by

boiroy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program 1 Sample program

#include<fstream.h> #include<iostream.h> int main() { Ofstream filename; filename.open(student.txt); filename<<Hello world; filename.close();

Program 2 Program to open a file


#include<fstream.h> #include<iostream.h> int main() { Ofstream filename(new.txt); if(filename.is!open()) { cout<<"he file is opened<<endl; else { cout<<"he file is not opened<<endl;

filename<<it is a new text file; filename.close();

Program 3 Program to write a file


#include<fstream.h> #include<iostream.h> int main() { Ofstream filename(studentinfo.txt); cout<<#nter stud$%& 'ame& (ar)s<<endl; cout<<*ress cntrl+, to -uit<<endl; int id; strin. name; int mar)s; while(cin>>id>>name>>mar)s) { filename<<id<</ 0; <<name<</ 0; <<mar)s<<endl;

Program 4 Program to read a file


#include<fstream.h> #include<iostream.h> int main() { ifstream filename(studentinfo.txt); int id; strin. name; int mar)s; while(filename>>id>>name>>mar)s) { cout<<id<</10 <<name<</10 <<mar)s<</1/<<endl;

Program 5 Program to get information from a file and display the information with the given choice (Initially you have to create a text file named objects as chair 0 pen 0 pencil 0 Alcohol - ! fuel -"0 #ruits !! medicines "!$ %xample&
#include<fstream.h> #include<iostream.h> int .etwhatuwant(); 2oid displa3items(int x); int main() { int whatuwant; whatuwant 4 .etwhatuwant(); while(whatuwant546) { switch(whatuwant) { case 78 displa3items(7); 9rea); case :8 displa3items(:); 9rea); case ;8 displa3items(;); 9rea); whatuwant 4 .etwhatuwant();

11.etwhatuwant function int .etwhatuwant() { int choice; cout<<7<plain items<<endl; cout<<:<harmful items<<endl; cout<<;<helpful items<<endl; cout<<6<plain items<<endl; cin>>choice; return choice;

11displa3item function 2oid displa3items(int x) { ifstream filename(o9=ects.txt); strin. name; dou9le 2alue; if(x447) { while(filename>>name>>2alue) { if(2alue44>) { cout<<name<<//<<num9er<<endl;

if(x44:) { while(filename>>name>>2alue) { if(2alue<>) { cout<<name<<//<<num9er<<endl;

if(x44:) { while(filename>>name>>2alue) { if(2alue>>) { cout<<name<<//<<num9er<<endl;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy