0

给定一个像“/documents/filename.txt”这样的字符串,我需要生成一个新字符串“/documents/filename_out.txt”,其中新字符串只是将_out附加到文件名,同时保留.txt后缀。

#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);
}

有没有办法,比如说,从字符串中删除最后 4 个字符 (.txt),然后在字符串后面附加“_out.txt”?

提前致谢!

4

5 回答 5

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

您需要确保缓冲区足够大以包含额外的 4 个字符。

于 2013-05-18T03:16:57.480 回答
1

这是针对您的问题的一个相当具体的解决方案。

#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);
}
于 2013-05-18T03:17:20.307 回答
0

使用函数strtok来标记您的输入并再次连接它们,每个标记之间都有 _out。

#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。我假设您的输入始终是正确的,因此它不会检查任何内容,除非通过使用 NULL 字符完成它来确保 outName 始终是正确的字符串。

于 2013-05-18T03:13:07.463 回答
0

谷歌是你的朋友。这是搜索查询的第一个命中:“c 替换字符串”。您需要的就在这里:在 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;
  }
}
于 2013-05-18T03:18:05.797 回答
0
char outName[sizeof(fileName)+4];
strcpy(outName, fileName);
strcpy(strstr(outName, ".txt"), "_out.txt");
//strcpy(strrchr(outName, '.'), "_out.txt");
于 2013-05-18T08:44:48.730 回答