By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,045 Members | 2,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,045 IT Pros & Developers. It's quick & easy.

getting New line character

P: n/a
Hi all,
I'm facing a wierd problem.
I've a file, which is getting updated every now and then. and i'm
having another program, which monitors the file. I've to read the file
line by line, and in the end, i've to find out whether the line has
end of line character ('\n') in it or not. if its not there (that
means the line is only partially written), i've to discard it.

I'm using std::string::getline() function, with the delimiter as '\n'.
But the problem is getline does not store the '\n' (new-line
character) in the output string it generates.

ifstream fin;
string s ;
getline (fin, s, '\n');

then s does not have '\n' character in it. (even though the original
line has). I've to find out whether the original line has new-line
character in it or not.

how to do that.

Thanks in advance,
SUrya
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Well I don't know how good is this solution but here it is
anyway:

ifstream fin; //I assume that you will put someting here(ifstream
fin("test,txt") or something)
string s ;
getline (fin, s, '\n');
s+="\n";

I think this would solve your problem

HTH

--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
Jul 22 '05 #2

P: n/a

"Surya Kiran" <sk*@fluent.co.in> wrote in message
news:59**************************@posting.google.c om...
Hi all,
I'm facing a wierd problem.
I've a file, which is getting updated every now and then. and i'm
having another program, which monitors the file. I've to read the file
line by line, and in the end, i've to find out whether the line has
end of line character ('\n') in it or not. if its not there (that
means the line is only partially written), i've to discard it.

I'm using std::string::getline() function, with the delimiter as '\n'.
But the problem is getline does not store the '\n' (new-line
character) in the output string it generates.

ifstream fin;
string s ;
getline (fin, s, '\n');

then s does not have '\n' character in it. (even though the original
line has). I've to find out whether the original line has new-line
character in it or not.

how to do that.


Simple enough. Read characters one at a time. Either you are going to hit a
newline or you are going to hit the end of a file.

bool read_line(istream& in, string& line)
{
line.resize(0); // empty existing string
char ch;
while (in.get(ch))
{
if (ch == '\n')
return true; // got whole line, return true
line += ch; // add char to line
}
return false; // hit end of file, must be partial line, return false
}

john
Jul 22 '05 #3

P: n/a
Well, there is a catch in using this. Actually i've solved it using
the same procedure, but some addition is there to it. ifstream by
default skips all the white spaces, including newline character also.
so by default, ch does not show any newline char, in the function. To
get the new line char, we've to prepend a line to the code.

char ch ;
in >> noskipws ;
... that will do.

Surya
Simple enough. Read characters one at a time. Either you are going to hit a
newline or you are going to hit the end of a file.

bool read_line(istream& in, string& line)
{
line.resize(0); // empty existing string
char ch;
while (in.get(ch))
{
if (ch == '\n')
return true; // got whole line, return true
line += ch; // add char to line
}
return false; // hit end of file, must be partial line, return false
}

john

Jul 22 '05 #4

P: n/a

"Surya Kiran" <sk*@fluent.co.in> wrote in message
news:59*************************@posting.google.co m...
Well, there is a catch in using this. Actually i've solved it using
the same procedure, but some addition is there to it. ifstream by
default skips all the white spaces, including newline character also.
so by default, ch does not show any newline char, in the function. To
get the new line char, we've to prepend a line to the code.

char ch ;
in >> noskipws ;
.. that will do.


Using in.get(ch) does not skip whitespace, using in >> ch can.

john
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.