Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 1st, 2007, 11:39 PM
Authorized User
 
Join Date: Jul 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem while file read

Hello,
 I think it is very silly, but i couldnt trace out the problem.
 I am trying to read 3 lines from file using fgets, then using fseek i m placing the pointer back one line.
 But after that when i do fgets i m not getting the complete line.Instead first 3 charecters are missing.

My code looks like

    for(i = 0; i < 3; i++)
        fgets(strBuf,500,fpInFileNext);

    cnt = strlen(strBuf);
    fseek(fpInFileNext,-(cnt),SEEK_CUR);
    fgets(strBuf,500,fpInFileNext); // now strBuf contains line with
                                         3 missing charecters.

What may be the problem?????????

Cheers,
Reply With Quote
  #2 (permalink)  
Old November 5th, 2007, 05:46 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm. How about just rewinding like:

fseek(fpInFileNext,-(cnt+3),SEEK_CUR);

I think the problem comes from new-line character (\r\n in Windows, \n in *nix) so you need to rewind past those also. I'm not sure where the third character comes from (too little coffeiine, can't think straight). If you are writing portable code you propably need to do some evil precompilter thingy:

#if _WIN32_
fseek(fpInFileNext,-(cnt+3),SEEK_CUR);
#else
fseek(fpInFileNext,-(cnt+2),SEEK_CUR);
#endif

Or something like that.

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
File read problem... john20 .NET Framework 2.0 4 September 9th, 2008 12:18 PM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
how to read file vivekkumar_23 Java Basics 1 April 3rd, 2006 10:03 AM
How to Read from INI file alen Pro VB 6 2 September 3rd, 2004 06:34 AM
can't read file nikolaosk BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 July 28th, 2003 04:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.