[try Beta version]
Not logged in

 
Finding new line in string

Jan 20, 2019 at 1:25am
I am Trying to find where there is a new line in a string in order to count which line is being output.
So far I have
1
2
3
4
5
for(int i=0; i<sizeoffile; i++){
if(firststring[i] == '\n' || secondstring[i]=='\n'){
linecount++;
}
}


When I do this, it doesn't end up incrementing linecount, the if statement must not happen.

Any ideas?
Jan 20, 2019 at 1:40am
Show more code.
Jan 20, 2019 at 1:55am
Are you sure that the line(s) contains only "\n" and not "\r\n"? If you're using Windows then your file may contain "\r\n" (CR-LF / carriage-return, line-feed) instead, in which case your test wouldn't work.
Last edited on Jan 20, 2019 at 12:49pm
Jan 20, 2019 at 4:00am
I tried using \r\n but that didn't work either.
Jan 20, 2019 at 4:59am
Are you sure that the line(s) contains only "\n" and not "\r\n"? If you're using Windows then your file may contain "\r\n" (CR-LF / carriage-return, line-feed) instead, in which case your test wouldn't work.


Whether there is a carriage return or not doesn't matter. \r and \n are two different characters so OP's code, since it searches for '\n', would work.

Univcplus you are looping through the file's size but are checking two strings? Can you show full code?
Jan 20, 2019 at 12:50pm
Yeah, you're right, of course. I should have looked at the code for more than a split second. That was careless of me!
Last edited on Jan 20, 2019 at 12:53pm
Topic archived. No new replies allowed.