Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / May 2005



Tip: Looking for answers? Try searching our database.

BSD tellg() not working?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dougn@tiac.net - 25 May 2005 22:01 GMT
I have a project which uses MSL_C++_BSD_C_Mach-O.lib and I've noticed
that tellg() returns -1 when I think it should be returning 0.  I've
tried the same code using the MSL C and it does return 0.  Here is a
snippet of what I'm doing.  I have my bsd paths before MSL.  Anyone
have any ideas?  Thanks -doug

#include <fstream>
#include <iostream>
#include <string.h>

int main(int argc, char * const argv[])
{
    std::ifstream    stream(argv[1]);

    unsigned long    pos;
    char            line[256];

    pos = stream.tellg();
    stream.read(line, 50);

    std::cout << "tellg: " << pos << std::endl;
    std::cout << "[" << line << "]" << std::endl;
    return 0;
}
Howard Hinnant - 26 May 2005 01:06 GMT
> I have a project which uses MSL_C++_BSD_C_Mach-O.lib and I've noticed
> that tellg() returns -1 when I think it should be returning 0.  I've
[quoted text clipped - 20 lines]
>     return 0;
> }

Are you sure that the file is getting opened in the first place?  
Default working directories can unfortunately differ between C libs.  If
stream isn't open, then tellg() would return -1.

-Howard
dougn@tiac.net - 26 May 2005 12:15 GMT
Yes, the file does open.  That's why I read a little of the file and
echo to the screen.
Howard Hinnant - 26 May 2005 15:14 GMT
> Yes, the file does open.  That's why I read a little of the file and
> echo to the screen.

Sorry, I'm having trouble duplicating your symptoms.  If the file
successfully opens, I keep getting a return of 0 from tellg() in your
example.

The only suggestion I'm coming up with at the moment is to initialize
your "line" to zero to ensure that you're not printing out garbage:

   char            line[256] = {0};

Assuming a successful open, the only calls to BSD C your program should
be making (under tellg) is:

fseek(file_, 0, SEEK_CUR);
ftell(file_);

(these calls are made in <msl_C_filebuf> in the function seek_device)

If either of those calls fail, then tellg() will return -1 as you
report.  But I currently do not see a reason for these calls to fail,
assuming of course that file_ points to a valid, open, FILE.

-Howard
dougn@tiac.net - 26 May 2005 16:01 GMT
Nope.  I placed a memset in there to zero out line and the file reads
ok but -1 is still being returned.    Maybe I have something messed
with my project.  Can I email you my project?
Howard Hinnant - 26 May 2005 18:41 GMT
> Nope.  I placed a memset in there to zero out line and the file reads
> ok but -1 is still being returned.    Maybe I have something messed
> with my project.  Can I email you my project?

Sure.

-Howard
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.