-
May 7th, 2011, 10:05 AM
#1
invoking tellg() gives a wrong output
inputFile "111":
Code:
919622058222 NOT CONNECTED
END
<hgsdp:msisdn=919906000234,suda;
HLR SUBSCRIBER DATA
code1:
Code:
#include<iostream>
#include<fstream>
int main(){
std::fstream ifs; std::string line;
ifs.open("111",std::fstream::in);
getline(ifs,line);
//int l=ifs.tellg();
getline(ifs,line);
std::cout<<'\n'<<line; getchar();
ifs.close();
}
o/p --> nothing (which is correct)
code2:
Code:
#include<iostream>
#include<fstream>
int main(){
std::fstream ifs; std::string line;
ifs.open("111",std::fstream::in);
getline(ifs,line);
int l=ifs.tellg();
getline(ifs,line);
std::cout<<'\n'<<line; getchar();
ifs.close();
}
o/p --> D
why is just invoking tellg() causing the unexpected shift in position?
{using MinGW (GCC 4.4.0) , Win. Vista}
-
May 7th, 2011, 11:29 AM
#2
Re: invoking tellg() gives a wrong output
why is just invoking tellg() causing the unexpected shift in position?
{using MinGW (GCC 4.4.0) , Win. Vista}
The tellg() function is intended for files opened in binary mode.
You opened the file in text mode, and doing so causes the file pointer to interpret carriage returns and linefeeds as a single character.
Regards,
Paul McKenzie
-
May 7th, 2011, 11:34 AM
#3
Re: invoking tellg() gives a wrong output
thanks...so is there a way around for the files opened in text mode?
-
May 7th, 2011, 11:36 AM
#4
Re: invoking tellg() gives a wrong output
Originally Posted by ustulation
thanks...so is there a way around for the files opened in text mode?
Why are you using tellg()? You want to go to an exact location in the file and do something, right?
So your only choice is to open the file in binary mode for the tellg() to work correctly. Playing around with tellg() with files opened in text mode will only result in you being frustrated why some things work, while other things do not.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 7th, 2011 at 11:39 AM.
-
May 7th, 2011, 01:08 PM
#5
Re: invoking tellg() gives a wrong output
Originally Posted by Paul McKenzie
So your only choice is to open the file in binary mode for the tellg() to work correctly.
so i'v made the transition...in binary mode getline reads \r of \r\n delimiters..so it took a hell lot of inspection to drop that off...also now i think the code will not work on Linux systems which handle EoL differently..so i'v to maintain two separate SC's
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|