Thread: need help about char

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    need help about char

    hi all
    how can i truncate this...
    1.2M /eee/uuu/123rwewv.zip

    in this i want only "1.2M"
    and
    i want only 123rwewv.zip(must be after last "/")

    can you help me please

    thank you in advance

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Perhaps you could try splitting the string into 2 separate components. I don't know how this could be done though (my known method is highly inefficient).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    is the text at 1.2M always going to be 4 characters? If so you could just copy the first 4 characters.

    To get the end part of the string you could:
    Get the line length.
    Read backwards through the line until you reach the first / and save the index
    Copy the section of that string after the index.
    Concatenate it to the first part of the string you copied.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    strchr() to find the first /
    strrchr() to find the last /
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using strchr and strrchr you can locate start and end of substrings you need and then
    strcpy for the last part and strncpy for the first
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    Quote Originally Posted by Salem View Post
    strchr() to find the first /
    strrchr() to find the last /
    sorry i did but, having some confusion..
    i tried like this
    char *s5, *split1, *file;
    //file is the above file path.
    s5=strrchr(file,"/");
    strcpy(split1,s5);

    can you please coding for me..

    and also i wanna read first five char... of the path.(1.2M /eee/uuu/123rwewv.zip)

    thank you in advance

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by munna_dude
    can you please coding for me..
    Sure, we'll tell you when it's done... And while your waiting you could read the rules ?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > s5=strrchr(file,"/");
    1. This generates some kind of error message right, something about pointer to int conversion?
    Use single quotes.

    Post actual code and error messages, not "something like these snippets"

    > strcpy(split1,s5);
    Where is split1 allocated?
    Is it allocated at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM