Thread: Moving a file pointer in a function

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Your solution doesn't work because the whole string is compared for the match, I need to know the index that the match is at so I can terminate the string if there is valid text before the comment.
    Since StrStr returns a pointer to the first character in the matched string, you do know exactly where to plop your \0 to terminate the string with only a very slight modification.

    Code:
    bool KillCommentString(char *str)
      { char *cmt; // address of comment
         cmt = strstr(str,Comment_Prefix);
         if (!cmt)
           return 0;
         *cmt = 0;  // terminate the string
         return 1; }
    No offense is intended, but I'm thinking you're still trying to make this whole new thing into the same old thing you already understand and, as I mentioned before, that's a mistake.
    Last edited by CommonTater; 05-04-2011 at 02:11 PM.

  2. #17
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Since StrStr returns a pointer to the first character in the matched string, you do know exactly where to plop your \0 to terminate the string with only a very slight modification.
    My mistake, I misunderstood how strstr was used, and I agree it would make much cleaner code, but I looked at an implementation of strstr and I think that although my solution is not beautiful by any measure it does have less overhead. This operation is - after all - preformed on every single character in the file.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    My mistake, I misunderstood how strstr was used, and I agree it would make much cleaner code, but I looked at an implementation of strstr and I think that although my solution is not beautiful by any measure it does have less overhead. This operation is - after all - preformed on every single character in the file.
    That may be true of some poorly implemented libraries. But the distribution libraries supplied with most better compilers are often highly optimized, often using inline Assembly to maximize their performance. So unless you're working with some really brain-dead library, it's very unlikely you will get better performance from self-made function replacements.

    But, you should suit yourself... these are your mistakes to make, my friend.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Both implementations are basically O(mn) where m is the length of the data in the file, and n is the length of your comment string. And unless you're developing on an Apple using their standard C library, there's no guarantee that the strstr you're environment provides is the same as Apples. It may be a much more optimized version. In fact, many of the standard library functions are highly optimized for the specific platform they're meant to be used on.

    Your attempt to justify this is touting preoptimization. You don't know whether the difference in speed is significant or negligible, or whether it causes some sort of bottle neck. The slowest part of your code is your reading from disk. Everything else is working at ludicrous speed compared to disk I/O. Besides, it's always preferable to use a well tested, existing, standard piece of library code than to invent the wheel. The only exception to that might be for purely academic purposes. In practical code this should be avoided at all costs.

  5. #20
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    ...Besides, it's always preferable to use a well tested, existing, standard piece of library code than to invent the wheel.

    ..So unless you're working with some really brain-dead library, it's very unlikely you will get better performance from self-made function replacements.
    Thanks for the perspective, I think I should study the standard library more so I do these kinds of things less.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Thanks for the perspective, I think I should study the standard library more so I do these kinds of things less.
    May I ask which compiler you're using? (you may have mentioned it before, but I don't recall...)

  7. #22
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    May I ask which compiler you're using? (you may have mentioned it before, but I don't recall...)
    I am using MinGW and Visual Studios

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    I am using MinGW and Visual Studios
    They're OK compilers ... for a minute there I was afraid you were going to come up with Turbo C or DEVC++... Whew!

    However; you should know that MSVS is not a standards based compiler... it's optimized by Microsoft for their own purposes.

  9. #24
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    However; you should know that MSVS is not a standards based compiler... it's optimized by Microsoft for their own purposes.
    Yea I know, I use it to check my code for compatibility issues.

    I am sure there will be more fixes like this...
    Code:
      #if USE_VISUAL_C
      #  define __func__ __FUNCTION__
      #  define inline __forceinline
      #endif
    Code:
    They're OK compilers
    EDIT: What in your opinion are better compilers? (also - although I use notepad++ with Mingw - I thought Dev C++ was an IDE on top of GCC. Am I wrong?)
    Last edited by 127.0.0.1; 05-04-2011 at 04:23 PM.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Yea I know, I use it to check my code for compatibility issues.

    I am sure there will be more fixes like this...
    Code:
      #if USE_VISUAL_C
      #  define __func__ __FUNCTION__
      #  define inline __forceinline
      #endif
    Orrrrrr... you could use minGW in C-99 mode checking standards as you program...
    Orrrrrr... you could switch to a C-99 standards based compiler like Pelles C (wonderful help file with that one!)

    Basically... using a non standards based compiler to check compatibility tends to *reduce* not improve portability of code. Moreover; unless you are writing open source software (Which I do on occasion, mostly libraries) portability to other compilers isn't an issue in the first place.

    Interestingly... in almost 7 years of C coding I've never used either inline or __func__ (and yes I know what both are).
    Last edited by CommonTater; 05-04-2011 at 04:27 PM. Reason: I have GOT to learn to proof read before posting....

  11. #26
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Orrrrrr... you could use minGW in C-99 mode checking standards as you program...
    I have since I started always used the C-99 switch.

    using a non standards based compiler to check compatibility tends to *reduce* not improve portability of code
    All of the code I produce is plain ANSI C-99, I only have these two fixes to make Visual Studios read the C-99 code.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good stuff...

    But ... you still gotta learn to think in smaller blobs...

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    EDIT: What in your opinion are better compilers? (also - although I use notepad++ with Mingw - I thought Dev C++ was an IDE on top of GCC. Am I wrong?)
    Not wrong... However, DEVC++ was abandoned by it's author nearly a decade ago and still distributes with the old compiler. GCC has been through several revisions --and improvements-- since then. Same with Turbo C... it was the next best thing when Fred Flintstone was still wearing diapers and even with a couple of updates it's still a 16 bit DOS based dinosaur... it's output won't even run on 64 bit systems.

    In my opinion, if you are doing C-99 standard programming your best bet is currently Pelles C. I say this for several reasons but mostly it's a very complete IDE with excellent documentation and a very tightly integrated compiler. Code::Blocks or Notepad++ and MinGW could give Pelles a run for the money if they had anything resembling Windows resource editors (Menus, Dialogs, String Tables, Message Tables, Accelerators, Manifests, etc) and full library documentation (press F1 on any keyword!).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. moving the pointer
    By ^ARTICUNO^ in forum C++ Programming
    Replies: 5
    Last Post: 02-06-2011, 12:25 PM
  2. Have function return file pointer
    By krogz in forum C Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  3. Pointer problem with FILE *f in function
    By mabuhay in forum C Programming
    Replies: 6
    Last Post: 02-14-2006, 04:15 PM
  4. moving an array from function to function
    By Dan17 in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2005, 04:07 PM
  5. Moving Mouse Pointer
    By loobian in forum Windows Programming
    Replies: 8
    Last Post: 10-16-2001, 03:45 PM