Thread: strlen()

  1. #1
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17

    strlen()

    I tried in the c++ forum but no one apparantly knows what to do.

    strlen() requires a const char, as in... strlen( const char );
    so, what do you do if you have a char* ?
    is there a way to get the text the pointer points to in to a const char?

    This is what I am trying to do...

    I need an array, lets say cText[256], to hold all the text in a text file. And I need to find strlen( cText ).


    but cText becomes a pointer when opening a file stream to it, and you can't use that in strlen(). I get a compile error about how I can't convert from char* to const char.



    No one responded to me in c++ programming forum, This is depressing, lol. Someone help me.

    If what I want can't be done exactly as I need, what are my alternatives?
    Last edited by exoeight; 03-31-2005 at 11:54 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    but cText becomes a pointer when opening a file stream to it
    Huh? I really don't understand what you mean there. You should be using something like FILE * to point to your file stream...

    Anyway, the strlen() thing is probably just a quirk with the compiler you're using (or maybe a C++ quirk?). Maybe you could try typecasting the buffer when you pass it to strlen (e.g. strlen((const char *)cText); )

    If you posted your code then people would probably be able to help you better.
    Last edited by itsme86; 03-31-2005 at 11:58 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17
    int maxClearText = strlen( (const char *)cClearText )

    error: "cast to pointer from integer of different size"

    source: http://www.simpleindesign.com/_encryption.cpp

    I have a file named test.txt with "this is a test" in it on my desktop. compile with 1 arguement, just the filename.
    Last edited by exoeight; 04-01-2005 at 12:02 AM.

  4. #4
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17
    i have no idea what i did, i threw around *'s and &'s and got it working some how....

    yay?!

    thanks though. you're tip did help me get started in the right direction.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What the parameters to strlen mean really is that strlen won't be chaning any values in the function itself. If you have a string, just pass it over to the function.
    Code:
    char *foo = "hello world";
    int x;
    
    ...
    
    x = strlen( foo );
    It's that simple.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17
    yeah, that does work, but it wasn't in the specific way I was using it.

    I had to do this... which still is a little flakey...

    ( key is a pointer pointing to a var that has a string in it passed through a function ).

    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[i] = *(key+i);

    then i could do:


    Code:
    maxCKey = strlen( cKey );
    hah.

    I guess if for some reason anyone else gets in to the situation I did, that's one way of doing it.


    edit:
    in the line: for( int i=0;i<strlen(key);i++ )

    sizeof(key) doesn't make all of the key show
    and strlen(key) loops way past actual key and makes funky symbols show.

    lol. awesome.
    Last edited by exoeight; 04-01-2005 at 08:23 AM.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by exoeight
    yeah, that does work, but it wasn't in the specific way I was using it.

    I had to do this... which still is a little flakey...

    ( key is a pointer pointing to a var that has a string in it passed through a function ).

    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[ i ] = *(key+i);

    then i could do:


    Code:
    maxCKey = strlen( cKey );
    hah.



    I guess if for some reason anyone else gets in to the situation I did, that's one way of doing it.


    edit:
    in the line: for( int i=0;i<strlen(key);i++ )

    sizeof(key) doesn't make all of the key show
    and strlen(key) loops way past actual key and makes funky symbols show.

    lol. awesome.
    First of all: you do realize that there is no such thing as a "string" data type in C, right?

    All standard C library "string" functions (strlen(), strcpy(), etc., including printf("%s")) operate on a null-terminated sequence of chars.

    1. The argument for strlen is (const *char), not (const char).

    2. If you use the name of an array of char in an expression, it is treated as a pointer to char.

    3. Your loop copies all of the chars up to but not including the terminating zero byte, so the result won't be useable with any standard C library string functions, including printf("%s"). So, your loop doesn't loop "way past the actual key", but trying to print the result with %s could go on forever.

    4. If key points to a null-terminated sequence of chars and the the number of chars (including the terminating null) is not greater than the size of the char array cKey, then you can use this to copy the key into your array:
    Code:
      strcpy(cKey, key)
    Regards,

    Dave

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by exoeight
    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[i] = *(key+i);
    then i could do:
    Code:
    maxCKey = strlen( cKey );
    The first looks like a homebrewed strcpy except for the null termination that would make the subsequent strlen work correctly.
    Last edited by Dave_Sinkula; 04-01-2005 at 09:14 AM. Reason: D'oh!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17
    Evans, thanks for you're post, probably most helpful comments i've gotten yet.
    What you said makes sense, and I have my program working better now as a result.

    Gotta mess up to learn, at least I tried, lol.

    Thanks again.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by exoeight

    Gotta mess up to learn, at least I tried, lol.

    Thanks again.
    Been there --- done that. (No one was born knowing this stuff, you know.)

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM