CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    16

    Arrow 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}

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    Join Date
    Mar 2011
    Posts
    16

    Re: invoking tellg() gives a wrong output

    thanks...so is there a way around for the files opened in text mode?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: invoking tellg() gives a wrong output

    Quote Originally Posted by ustulation View Post
    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.

  5. #5
    Join Date
    Mar 2011
    Posts
    16

    Re: invoking tellg() gives a wrong output

    Quote Originally Posted by Paul McKenzie View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured