C++ Language



Admission Enquiry Form

  

Use of Seekp() and tellp() in C++


Write and Read Data into the File using different ways:

Write string into the file and use of is_open() function.

//program to write data in file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("compuhelp.txt");
cout<<endl<<"status of file : "<<fout.is_open();//it will return 1 or 0
if(fout.is_open())
{
fout<<"This is first line"<<endl<<"This is second line";
fout<<"\n This is third line";

   }//end of if statement
else
{
cout<<endl<<"Could not open file";
}
fout.close();//closing file

}//end of main




Code to write different type of data into the file.

//Writes different type of data in file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int inum1,inum2;
float fnum1,fnum2;
char str[50];
ofstream fout;
fout.open("c:\\students\\compuhelp.txt");
cout<<"Enter integer numbers:";
cin>>inum1>>inum2;

cout<<"Enter float numbers:";
cin>>fnum1>>fnum2;

cout<<"Enter string without space:";
cin>>str;

  if(fout.is_open())
{
fout<<inum1<<" "<<inum2<<endl;
fout<<fnum1<<" "<<fnum2<<endl;
fout<<str;
cout<<endl<<"Data saved into the file";

   }//end of if statement
else
{
cout<<endl<<"Could not open file";
}
fout.close();//closing file

}//end of main




Write 3 characters into the file one by one and using seekp() and tellp() functions.

//writes 3 characters into the file one by one
//and using seekp() and tellp() functions
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
char ch='s';
fout.open("c:\\students\\compuhelp.txt");
if(fout.is_open())
{
fout.put(ch);//writes a character into the file
fout.put(ch);
fout.put(ch);
cout<<endl<<"Data saved to file";
cout<<endl<<"file Cursor position is "<<fout.tellp();
//output 3 position starts from 0
fout.seekp(1);//sets file pointer or cursor to 2 position
fout.put('g');//writes at 2nd pos, output :sgs
fout.close();
}
else
{
cout<<endl<<"Could not open file";
}
}//end of main




Saving data members of the class into the file.

//Saving data members of class to file
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
char name[50];
int rollno;
float fee;
ofstream fout;
public:
void setData()
{
cout<<"Enter name of student:";
cin>>name;
cout<<"Enter rollno of student:";
cin>>rollno;
cout<<"Enter fee of the student:";
cin>>fee;
}//end of function
void saveData()
{
fout.open("c:\\students\\compuhelp.txt");
if(fout.is_open())
{
fout<<name<<" "<<rollno<<" "<<fee;
cout<<endl<<"Data saved to file";
fout.close();
}
else
{
cout<<endl<<"Could not open file";
}
}//end of function
};//end of class

int main()
{
Student st;//creating object of class Student
st.setData();
st.saveData();
}//end of main




Saving data members of the class using write() function into the file.

//Saving data members of class using write function to file
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
char name[50];
int rollno;
float fee;
ofstream fout;
public:
void setData()
{
cout<<"Enter name of student:";
cin>>name;
cout<<"Enter rollno of student:";
cin>>rollno;
cout<<"Enter fee of the student:";
cin>>fee;
}//end of function
void saveData()
{
fout.open("c:\\students\\compuhelp.txt");
if(fout.is_open())
{
fout<<name<<" "<<rollno<<" "<<fee;
cout<<endl<<"Data saved to file";
fout.close();
}
else
{
cout<<endl<<"Could not open file";
}
}//end of function
};//end of class

int main()
{
Student st;//creating object of class Student
st.setData();
ofstream fout;
fout.open("c:\\students\\compuhelp.txt");
if(fout.is_open())
{
fout.write((char*)&st,sizeof(st));//writing block of data using address
cout<<endl<<"Data saved to file";
fout.close();
}
else
{
cout<<endl<<"Could not open file";
}

}//end of main