How to convert tabs to spaces in a text file with C++

1 Answer

0 votes
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;

int main()
{
	std::ifstream ifs("d:\\data.txt");
	char ch;
	std::string s;

	while (!ifs.eof()) {
		ifs.get(ch);
		if (ch == '\t')
			s += " ";
		else
			s += ch;
	}

	ifs.close();

	std::ofstream ofs("d:\\data.txt");
	ofs << s;
	ofs.close();

	return 0;
}

/*
run:

data.txt
--------
c c++
c# java  php python

*/

 



answered Jun 14, 2018 by avibootz

Related questions

1 answer 244 views
1 answer 268 views
1 answer 134 views
134 views asked Jun 8, 2018 by avibootz
...