Thread: Character in a String

  1. #1
    Registered User DutchStud's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Question Character in a String

    I'm trying to make a function that turns a filename into a shorter name. For example "c:\windows\clouds.bmp" to "clouds". The problem comes at sname[s]=c; Does anyone know what the problem is? I have no idea. It doesn't give me a compliler error, but I get an assembly error (I use borland).

    Code:
    void ShortenFileName(char fname[0xff], char sname[0xff]) {
        int f=0, s=0;
        char c;
        do {
            c = fname[f];
            if (c=='.' || c=='\0') {
                sname[s]='\0';
                return;
            }
            sname[s]=c;
            s++;
            if (c=='\\') {
                s=0;
            }
        }
        while(true);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would do

    Code:
    void ShortenFileName(char *fname, char *sname)
    {
         int iIndex=0;
    
         while((fname[iIndex] != '.' )&&( fname[iIndex] != '\0' ))
         {
             sname[iIndex]=fname[iIndex];
             iIndex++;
         }
         sname[iIndex]='\0';
         return;
    }
    or
    Code:
    void ShortenFileName(char *fname, char *sname)
    {
         char    *pString=NULL,*pLastSlash=NULL;
         
         //find the first \
         pString=strstr(fname,"\");
         if(pString!=NULL)//found one look for more
         {
            do
            {
                 pLastSlash=pString;
                 pString=strstr(pString,"\");       
            }
            while(pString!=NULL)
            pLastSlash++; //now should be just after last back slash
         }
         else  pLastSlash=fname[0]; //no back slash so set to start
         pString=sname[0];
         while(pLastSlash != '.' )&&( pLastSlash != '\0' ))
         {
             *pString++=*pLastSlash++;
         }
         return;
    }
    (or something on these lines, I have not tested this)
    Last edited by novacain; 12-12-2001 at 11:18 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Remember that you must write '\\' instead of '\'.
    // Gliptic

  4. #4
    Registered User DutchStud's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Question why...

    So why won't it let me use sname[s]=c;
    Why do you increment the pointers? (*pString++=*pLastSlash++;)

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The pointers are incremented so one string is copied into the other and the while has something new to test. They are just an index to each string.

    I think that you need to use a pointer if trying to make an element of a string array = another char variable.

    Or

    as the char not terminated may read past the single character into the next memory location. (treats the char "c" as an array ie string)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User DutchStud's Avatar
    Join Date
    Oct 2001
    Posts
    43
    I used the function you gave me, and it gave me the same type of error, not in compiling but in debugging. it gave me it where you incremented both pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM