0

Given a string like "/documents/filename.txt", I need to produce a new string "/documents/filename_out.txt", where the new string simply appends _out to the filename, while preserving the .txt suffix.

#include <stdio.h>
#include <string.h>

int main(){;
    char fileName[80];
    printf("Please enter filename: ");
    scanf("%s", fileName);
    char outName[];

    printf("Outputname: %s\n", outName);
}

Is there a way to, say, remove the last 4 characters (.txt) from a string, then append the string with "_out.txt"?

thanks in advance!

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • 2
    What's wrong with using `str{,n}{cpy,cat}()`? – Ignacio Vazquez-Abrams May 18 '13 at 03:11
  • Yes, there's a way to do that. How else can we help you? Questions that ask "Is it possible?" or "Is there a way?" are not answerable here except by saying "Yes, it is" or "No, it isn't", neither of which are helpful to anyone. Please [edit] your question to make it more specific. – Ken White May 18 '13 at 03:13
  • It seems to be solved here: http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c – carlosdc May 18 '13 at 03:15

5 Answers5

1
// strlen(".txt") = 4    
strcpy(filename + strlen(filename) - 4, "_out.txt");

You need make sure the buffer is big enough to contain the extra 4 more characters.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
1

Here's a fairly specific solution to your problem.

#include <stdio.h>
#include <string.h>

int main(){

    char newSuffix[] = "_out.txt";

    char fileName[80];
    printf("Please enter filename: ");
    scanf("%s", fileName);
    char outName[85];

    strcpy(outName, fileName);
    strcpy(&outName[strlen(fileName) - 4], newSuffix);

    printf("Outputname: %s\n", outName);
}
kgraney
  • 1,975
  • 16
  • 20
0

Use function strtok to tokenize your input and the concatenate them again, with _out between each token.

#include <stdio.h>
#include <string.h>

int main(){;
char fileName[80];
printf("Please enter filename: ");
scanf("%s", fileName);
char outName[80];
char *name, *type;

name = strtok(fileName, ".");
type = strtok(NULL, ".");

snprintf(outName, 80, "%s_out.%s", name, type);
outName[79] ='\0';


printf("Outputname: %s\n", outName);
}

PS. I'm assuming the your input is always correct, so it isn't checking against anything except making sure that outName will always be a correct string by finishing it with NULL character.

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • thats a huge sledge hammer to crack a tiny nut. The strstr method is much much simpler. – Steve Atkinson May 18 '13 at 09:01
  • @V-X This just a prove of concept, not a correct one in the sense to be safe at production time – Amadeus May 18 '13 at 19:39
  • @Steve Atkinson If you could learn a generic one that solve most of problem, and is simple to implement, why to be using one that you will change it very soom? – Amadeus May 18 '13 at 19:40
  • question is if the sledge hammer is able to hit it's target (imagine what may happen, when there are two dots in the filename) – V-X May 19 '13 at 09:22
  • So, all the others solutions suffer from the same problem. – Amadeus May 19 '13 at 11:00
0

Google is your friend. It is the first hit to the search query: "c substitute string". What you need is right here: What is the function to replace string in C?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
   char fileName[80];
   printf("Please enter filename: ");
   scanf("%s", fileName);
   char outName[];
   printf("Outputname: %s\n", replace(filename,".txt","_out.txt"));
}


char * replace(
    char const * const original, 
    char const * const pattern, 
    char const * const replacement
) {
  size_t const replen = strlen(replacement);
  size_t const patlen = strlen(pattern);
  size_t const orilen = strlen(original);

  size_t patcnt = 0;
  const char * oriptr;
  const char * patloc;

  // find how many times the pattern occurs in the original string
  for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  {
    patcnt++;
  }

  {
    // allocate memory for the new string
    size_t const retlen = orilen + patcnt * (replen - patlen);
    char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );

    if (returned != NULL)
    {
      // copy the original string, 
      // replacing all the instances of the pattern
      char * retptr = returned;
      for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
      {
        size_t const skplen = patloc - oriptr;
        // copy the section until the occurence of the pattern
        strncpy(retptr, oriptr, skplen);
        retptr += skplen;
        // copy the replacement 
        strncpy(retptr, replacement, replen);
        retptr += replen;
      }
      // copy the rest of the string.
      strcpy(retptr, oriptr);
    }
    return returned;
  }
}
Community
  • 1
  • 1
carlosdc
  • 12,022
  • 4
  • 45
  • 62
0
char outName[sizeof(fileName)+4];
strcpy(outName, fileName);
strcpy(strstr(outName, ".txt"), "_out.txt");
//strcpy(strrchr(outName, '.'), "_out.txt");
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70