Reading from a file

Hi, is there any way that I can read from a file until I encounter a \t character and then continue reading the same line, ignoring the character?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	//first store info in a file where we want to read it without the "\t"'s
	ofstream createfile("test.txt");
	createfile << "zaryabsaeed \t wants \t to \t ignore \t the \t tabs. \t why \t does \t zryabsaeed \t hate \t the \t tabs?\r\n it's \tjust \tnot \tfair. \r\nthe \ttabs \t are being \tdiscriminated \t\t against.";
	createfile.close();

	string filetext; //Stores all the text received from the file
	ifstream readfile("test.txt");
	char c; //Holds the individual character we will read from the file
	readfile.read(&c, 1); //Read 1 character from the file
	while (!readfile.eof()) //while not at the end of the file
	{
		if (c != '\t') //if character is not \t
			filetext += c; //add character to our string
		readfile.read(&c, 1); //get next character (if it exists)
	}
	readfile.close(); //close our file when we reach the end
	cout << filetext << endl; //print out our file
	getline(cin, filetext); //call getline to stop the program from closing automatically (not necessary)
	return 0;
}
Last edited on
This is a good solution but i would think that it would be better to just write the psuedocode and let him figure it out. That way he would be able to remember it a little better when he runs across this issue again.

This is only my suggestion.
Topic archived. No new replies allowed.