String Manipulation.

Started by
4 comments, last by Bugdude 19 years, 8 months ago
Hello there. I have a small problem which I am sure many of you will know the answer to of the top of your heads but which is giving me some trouble. If I have a file name stored as a WCHAR* such as: "pics\pic1.bmp" How do I manipulate this to become: "pics\pic1a.bmp" In other words just insert an 'a' before the file extension. The file extension will always be the same size, but the file name may change in size if that helps with the solution. Thanks in advance. Mark Coleman
Advertisement
You could strtok at "." and add to the end.

char *pic = "pics\pic1.bmp";char newletter = 'a';char *postfix = "bmp";char seps = '.';char *tok;tok = strtok(pic, seps); //find our first tokenif(token != NULL){   realloc(pic, sizeof(pic)+sizeof(char)); //make it so we can add another character   sprintf(pic, "%s%c.%s", token, newletter, postfix); //add that other character}else{  //we couldn't strtok   exit(1);}


That should do the trick, as long as there are no extra periods. Which their shouldn't be.
Several different ways to do it. One way is to copy the first 'bit' of the name to a temp buffer, attach the 'a' then re-attach the extension. Example:

#define EXT ".bmp" char* string = "pic/pic1.bmp"; // Create a new string that is big enough to hold new name. char* newString = new char[strlen( string+2 )]; // copy everything except the extenstion. strncpy( newString, string, strlen(string)-strlen(EXT) ); // Add on the 'a' strcat( newString, "a" ); // Add the extenstion back on. strcat( newString, EXT ); //  printf("New String: %s\n", newString );


--------
Andrew
Hello,

You handle wchat_t strings just as you handle char strings. There is no logical difference between them. The only thing you'll have to do is to change your base type to wchar_t instead of char. Of course, it means that you cannot use the classical c string functions (strcpy, strlen, etc) but you may have the w equivalent (AFAIR the VC++ ones are wcscpy(), wcslen(), and so on). Check your compiler documentation for further informations.

For example, this is a function which takes a filename as input and adds a letter before the extention (simple one, no error checking, can crash id the filename is illformed, and so on, result must have been allocated and badly written...)

void add_letter_before_extention(wchar_t *result, wchar_t *filename, wchar_t ch){    int l = wcslen(filename);    int p = l-1;    int i;    result[0] = 0;    while (p>=0&&filename!=(wchar_t)('.')) p--;    if (p<=0) return;    for (i=0; i<p; i++) result = filename;    result[l] = ch;    for (i=p; i<=l; i++) result[i+1] = filename;}
maybe this as an alternative:

#include <string>#include <iostream>inline void c_style_string(const wchar_t* const c) {  std::wcout << c << std::endl;}int main() {   std::wstring s(L"pics\\pic1.bmp");   std::wstring::size_type i = s.find_last_of(L'.');   s.insert(i, L"a");   c_style_string(s.c_str());   return 0;}
It seems everyone is going to put in their 2 cents, so heres mine.

WCHAR source[] = L"pics\\pic1.bmp";size_t len = wcslen(source);WCHAR *result = new WCHAR[++len + 1];wcscpy(result, source);WCHAR *tmp = wcsrchr(result, '.');memcpy(tmp + 1, tmp, wcslen(tmp) * sizeof(WCHAR));*tmp = 'a';result[len] = '\0';


or the easy way out,

std::wstring result = L"pics\\pic1.bmp";size_t loc = result.rfind('.');result.insert(loc, 1, 'a');

This topic is closed to new replies.

Advertisement