Thread: a simple string operation has turned into a cluster.. SOS

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    a simple string operation has turned into a cluster.. SOS

    I have tried every possible combination of string operators ( I think) to split a char array that is to be used as a filename variable. What I need to do is split the user entered filename around the period using dino dos's 8.3 file format, so the period is the deliminator (I guess). However, if the first half of the file name is less than 8 characters, the strxfrm(desiredhalf, enteredhalf, 8) won't work. The strcpy(1,2) is of no practical use. gets, puts won't work right. I am going crazy over this seemingly simple operation. What am I missing? Any help would be greatly appreciated. This is being done on a 16 bit compiler for pure dos w/o the string operator being used for variable names.

    char filename[13]; // holder for first 8 chars
    char fileName[13]; // user filename in 8.3 format
    char fileext[13]; // holder for last 3 chars... should be [4] but.....

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Registered User bobthefish3's Avatar
    Join Date
    Oct 2001
    Posts
    22

    Lightbulb hopefully this'll help a bit...

    hey, i may have found a way that might work for what you are trying to do, i looked at the textbook i use for my college C course and i would suggest this:

    use the function strstr(s1,s2) from the string.h header. This function searches for the first occurrence of the string <s2> that is found in the string <s1>. if it is found it returns a pointer to the start of where that string is located inside of <s1>. and if it's not found it returns a null pointer.
    this way you could search for the string "." the char array holding the filename and then if it's found just increment the pointer by 1 and then from that point to the end of the string will be the extension, and if you wanted the first part you could probably do something like this:

    first search through the string with strstr to make sure the user entered a filename with a period in it. then you could run through it a second time, but while you are running through it, you could copy each character 1 by 1 into a different char array (to hold the file name without the .xxx extension on it) and just keep copying the characters until you find that first occurence of the period (basically loop through- incrementing a pointer and copying the character each iteration of the loop until the loops counter is equal to the pointer where the instance of the period is.

    I hope that works, i've never tried it before, and i'm actually not even using a compiler to test it or anything, it just seemed like an idea that i came up with (looking at my books) that seems like it logically and syntactically should work out (although there is most likely a simpler way).

    Hope it helps -
    -please do not mess with the incredible radioactive super-fish... thank you.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Re: a simple string operation has turned into a cluster.. SOS

    /* this code assumes that the user entered fileName is a NULL terminated string*/

    char filename[13]; // holder for first 8 chars
    char fileName[13]; // user filename in 8.3 format
    char fileext[13]; // holder for last 3 chars

    enum {FALSE, TRUE};
    int decimalEncountered = FALSE;
    char ch;
    int i, j;


    /*Enter the getting-string code here.
    After you get the string:*/

    for (i=0, j=0; fileName[i]; i++) {
    if (fileName[i]=='.') {
    decimalEncountered = TRUE;
    filename[i] = '\0';
    } else {
    if (decimalEncountered) {
    fileext[j] = fileName[i];
    j++;
    } else {
    filename[i] = fileName[i];
    }
    }
    }
    fileext[j] = '\0';


    Hope This Helps,
    Joshua Burkholder

  4. #4
    Registered User bobthefish3's Avatar
    Join Date
    Oct 2001
    Posts
    22

    Thumbs up good call on a easier way to make that string function work...

    good call my man, now i slap myself seeing how easy it could have been, o well my way still gets some style points <knudge!>

    -please do not mess with the incredible radioactive super-fish... thank you.

  5. #5
    Unregistered
    Guest
    While that code with the TRUE/FALSE enum was, well, decent, this code is a little better.

    int main()
    {
    char *p;

    ...
    p=strchr(filename, '.');
    /* p now points to a place in filename
    * where the character is the '.' character
    * or NULL if one doesn't exist
    */
    }

    this code is much shorter and probably much faster...

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Thanks to all. It looks like a good place to start.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive string operation..
    By transgalactic2 in forum C Programming
    Replies: 72
    Last Post: 01-09-2009, 04:42 PM
  2. Simple problem with string... Please take a look
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-03-2006, 06:48 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. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM