gpt4 book ai didi

c - 我需要一种编辑字符串的方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:56:41 24 4
gpt4 key购买 nike

给定一个类似“/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”?

提前致谢!

最佳答案

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

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

关于c - 我需要一种编辑字符串的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16620296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com