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
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 -
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
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!>
:D