getting New line character

S

Surya Kiran

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
 
F

Frane Roje

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
 
J

John Harrison

Surya Kiran said:
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
 
S

Surya Kiran

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
 
J

John Harrison

Surya Kiran said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top