Answer to Question #274043 in C++ for Neil

Question #274043

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(_). 


1
Expert's answer
2021-12-02T14:16:36-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment