Q1. Write the statements using seekg() to achieve the following:
(i) To move the pointer by 15 positions backward from current position.
(ii) To go to the beginning after an operation is over.
(iii) To go backward by 20 bytes from the end.
(iv) To go to byte number 50 in the file.
Q2. Write a Program in C++ that will read a line of text containing more than three words and then replace all the blank spaces with an underscore(_).
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream inf("text.txt");
if (!inf)
{
std::cerr << "Uh oh, text.txt could not be opened for reading!" << std::endl;
exit(1);
}
std::string strdata;
getline(inf, strdata);
std::cout << strdata << std::endl;
inf.seekg(-15, std::ios::cur);
std::getline(inf, strdata);
std::cout << strdata << std::endl;
inf.seekg(-23, std::ios::end);
getline(inf, strdata);
std::cout << strdata << std::endl;
inf.seekg(20, std::ios::beg);
getline(inf, strdata);
std::cout << strdata << std::endl;
inf.seekg(0, std::ios::beg);
getline(inf, strdata);
std::cout << strdata << std::endl;
int count = 0;
char ch = ' ';
while (ch != '\0')
{
ch = strdata[count++];
if (ch == ' ')
{
strdata[count - 1] = '_';
}
}
std::cout << strdata << std::endl;
return 0;
}
Comments
Leave a comment