getline to stop at an integer

Hey guys

I'm having a small problem with the getline function, i can't think of a command that will do this:

Let's suppose i have a basic code that takes data from a file 'file1' and outputs it into a file file2.
now what i want to happen is, let's say the sentence in file1 (or the input) is:

hello i am testing 1

but the integer 1 can be any random integer.
what im trying to do is to get the getline function to stop at 1 (but i dont want to say getline(ind,sentence,'1'), but instead of "1" i want it to accept any integer. I don't know if i made myself very clear, but is that feasible?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
	ifstream ind;
	ofstream outd;
	ind.open("file1.txt");
	outd.open("file2.txt");
	while (!ind || !outd)
	{
		cout << "ERROR" << endl;
		return 1;
	}
	string sentence;
	getline(ind, sentence);
	outd << "Sentence is: " << sentence << endl;
	ind.close();
	outd.close();
	return 0;
}


thank you.
An easier way to do this I think would be using ifstream.peek(), "if(...)" check and the ">>" operator. You would be reading the data as delimited by white space but that shouldn't make a difference from what I see in your code. Also in order to do this you would need to read the data in with a loop.

EDIT:
See Here:
http://www.cplusplus.com/reference/iostream/istream/peek/
and
http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/
Last edited on
Topic archived. No new replies allowed.