Making a dynamic char * thingie O_o?

This is a discussion on Making a dynamic char * thingie O_o? within the Linux Programming forums, part of the Platform Specific Boards category; Hi everyone, I have been trying to convert an integer into a string, and that integer will be the name ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Hi everyone,

    I have been trying to convert an integer into a string, and that integer will be the name of the file. This is the solution I have so far.. but it's not dynamic and it can't exceed 50 !.. I want a string that will fit exactly the integer I am converting.. Any Ideas?

    char HashIdx [50]; // <--- I want that to be dynamic.. not fixed to 50 in size.
    sprintf (HashIdx, "%i.idx", Hash);
    ofstream out(HashIdx, ios:ut | ios::binary);

    Thanks in advance,

    Ed.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you think it needs to be dynamic. 50 is far more than an integer can express (about 12 digits).

    You could of course use stringstream instead of sprintf, and then extract the buffer as a string, and use c_str() to get the filename to ofstream.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, if you really want to then just:
    Code:
    char HashIdx [50]; // <--- I want that to be dynamic.. not fixed to 50 in size.
    sprintf (HashIdx, "&#37;i.idx", Hash);
    char buf[strlen(HashIdX)+1];
    ofstream out(buf, ios:ut | ios::binary);
    EDIT: If for whatever reason you have memory limitations, either use malloc so you can free, or just use the maximum number of digits for an int
    Last edited by C_ntua; 09-24-2008 at 09:25 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Well, if you really want to then just:
    Code:
    char HashIdx [50]; // <--- I want that to be dynamic.. not fixed to 50 in size.
    sprintf (HashIdx, "&#37;i.idx", Hash);
    char buf[strlen(HashIdX)+1];
    strcpy(buf, HashIdx);
    ofstream out(buf, ios:ut | ios::binary);
    Red code added to correct it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Cool. thanks guys...
    Why do you think it needs to be dynamic. 50 is far more than an integer can express (about 12 digits).
    I guess I don't really need to, but I was kind of curious. Might need to do this in another occasion, when they need to know the length of the string.
    Someone else, told me to use boost libraries for this lexical_cast. So I might give that a try too.
    Last edited by afflictedd2; 09-24-2008 at 09:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21