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.
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.
strlen() returns the number of characters before the nul terminator
And a character has at least 8 bits, not bytes.
"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 likeso 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.Code:printf("value is '%s'\n", myString);
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.
How are you reading the text? It's probably the newline, control feed and/or EOF.
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"
my problem
hash algorithm only works if strlen(input) = the number of characters in the fileCode: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
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?
Haha, of course, that works, thanks...thought fread was void type.