Thread: character arrays

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    character arrays

    Please help me understand. I read a char*input from a text file with "aa" as text. If each char is 8 bytes, how can it be that strlen(input) is not equal to 16, but 5? Thanks.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    strlen() returns the number of characters before the nul terminator
    And a character has at least 8 bits, not bytes.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    "char" is always 1 byte in size. "w_char" is longer, but thats irrelevant. Remember that a "string" is always terminated (or at least always should be) with the null terminating character '\0', so the actual length of the string should always be one more than what you "see". So if you read the line "aa" in your file, you may have actually read "aa\n" or "aa ", etc. , depending on what your file exactly contains and on how you read the file. Also, there will be the addition terminating character. So your "char*" might actually look like "aa \n\0" or something, in which case it has 5 characters in it. However, "strlen" returns the size of the string less one, for the terminating character, so that it would report length "4".

    If its printing 5 then it must have 5 characters in it, excluding the terminating character. I would try something like
    Code:
    printf("value is '%s'\n", myString);
    so that you can tell what is in the string, since it is surrounded by quotes. If you print it without quotes or some other delimiter, the string may actually contain trailing spaces which you couldnt otherwise tell.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    OK I see when I print, it looks like notepad appends some random character to the end of the file. Any idea what is going on with that character? Thank you so much for helping.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    How are you reading the text? It's probably the newline, control feed and/or EOF.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    I read the text using fread(input,1,512,file). It is odd though that if I use "aaaaaaaaa", the input is equal to "aaaaaaaastem;C:\program files..."???? It works fine with some input like "aaaaa"

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    my problem
    Code:
        input = malloc(512);
        fread(input,1,512,file);
    
        printf("%d\n", strlen(input));
        printf("%s\n", input);
        
        fclose(file);
        SHA1Reset(&context);                             // initialize SHA1Context
        SHA1Input(&context, input, strlen(input));         // store input into context
        SHA1Result(&context);             // calculate result
    hash algorithm only works if strlen(input) = the number of characters in the file

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fread() does not NUL terminate the buffer, so using strlen() on the read buffer is dangerous and silly. fread() returns the number of "elements" read, why not just use that?

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    Haha, of course, that works, thanks...thought fread was void type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM

Tags for this Thread