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

Stream

The document contains several C++ code snippets demonstrating file handling, including writing and reading data to and from text files and binary files. It shows how to store student grades, read integers from a binary file, and write names to a text file. Additionally, it includes examples of using file pointers to manipulate file positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Stream

The document contains several C++ code snippets demonstrating file handling, including writing and reading data to and from text files and binary files. It shows how to store student grades, read integers from a binary file, and write names to a text file. Additionally, it includes examples of using file pointers to manipulate file positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<iostream.

h>
#include<fstream.h>
#include<stdlib.h>
struct stud{
int mid;
int final;
int total;
}student;
int main()
{
fstream data;
data.open("C:\\binary.txt",ios::in|ios::out|
ios::app);
if(data.fail())
{
cout<<"\n eror";
exit(1);
}
for(int i=0; i<3; i++){
cout<<"\n enter the mid";
cin>>student.mid;
cout<<"\n enter the final";
cin>>student.final;
student.total=student.mid+student.final;
//data.write((char*)&student,sizeof(student));
//data.read((char*)&student,sizeof(student));
cout<<student.mid<<endl;
cout<<student.final<<endl;
student.total=student.mid+student.final;
cout<<student.total<<endl;
cout<<"======"<<endl;
data<<student.mid<<endl;//to write on text fil
data<<student.final<<endl;
data<<student.total<<endl;
data<<"++++"<<endl;
}
data.close();
}

1
Example to read files

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int main()
{
fstream fp;
int i;
int sum=0;
int fileSize;
fp.open("C:\\binary.txt",ios::binary|ios::in|ios::out);
if(fp.fail())
{
cerr<<"\n error in openning files";
exit(1);
}/*
for(int j=0;j<5;j++){
cin>>i;
fp.write((char*)&i,sizeof(int));
}*/
while(!fp.eof())
{
fp.read((char*)&i,sizeof(int));
sum=sum+i;
cout<<i<<endl;
}
cout<<sum;
fp.close();
}

2
Stream Example To write Data to name.txt
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
ofstream fp;
int main()
{
fp.open("C:\\name.txt",ios::out);
if(fp.fail())
{
cerr<<"\n error in openning files";
exit(1);
}
fp<<" Abebe Alemu"<<endl;
fp<<" Alemu Tefera"<<endl;
fp<<" Tesfaye Mulugeta"<<endl;
fp<<" Mahlete Kebede"<<endl;
fp<<" Tesema bekele"<<endl;
fp<<" Belete Geremew"<<endl;
fp.close();
}
File pointer Example(seek gang tellg)
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
fstream fp;
int main()
{
char c;
fp.open("C:\\name.txt",ios::out|
ios::in);
if(fp.fail())
{
cerr<<"\n error in openning files";
exit(1);
}
for(int c='A';c<='Z';c++)
{
//cin>>c;
fp.put(c);
}
3
fp.seekg(8,ios::beg);
fp>>c;
cout<<" the 8th element is\n"<<c;
fp.close();
}

You might also like