Results 1 to 5 of 5

Thread: Real length of a char string?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    42

    Real length of a char string?

    The length of a character string is usually identified by the first null byte. But what if you had read a binary file? Files are filled with null bytes. I know fread returns the amount of bytes read, but is there another way?

    So strlen("123456") returns 6;

    strlen("123456\07"), that is 1,2,3,4,5,6,NULLBYTE,7, and that returns 6. Any otehr way?
    Last edited by hellowonn; Jul 20th, 2008 at 08:46 AM.

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Real length of a char string?

    If your main concern is binary files there are usually platform specific APIs for counting the length of files.. which are basically strlen that ignores null bytes.

    You could probably find a function on the internet with little trouble that counts strings without looking for a null byte.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Real length of a char string?

    So you mean basically the length of the file? Look up fseek() and ftell().
    I don't live here any more.

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    42

    Re: Real length of a char string?

    No, I just used that as an example.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Real length of a char string?

    Then the answer to your question is implementation-specific and you'll have to do some research.
    I don't live here any more.

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