gpt4 book ai didi

c++ - 具有操作数据重置功能的字符数组

转载 作者:行者123 更新时间:2023-11-30 19:15:57 26 4
gpt4 key购买 nike

我编写了一个简单的函数,它将获取带有扩展名的文件名并返回扩展名和减去扩展名的文件名。它工作正常,但当我测试它时,我发现我的一些文件名正在重置。并不总是,但有时。

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

#define EXTENSION_STR_MAX (16)
#define METADATA_STR_MAX (129)
struct meta_data_t
{
char filename[METADATA_STR_MAX];
char fileextension[EXTENSION_STR_MAX];
};

void GetFileExtMetadata(const char* fileName, char* fileExt, char* updateFileName)
{
// NULL Checks
if((NULL == fileName) || (NULL == fileExt) || (NULL == updateFileName))
{
printf("NULL :-/ \n");
return;
}

// Find the position of the last dot
char * pch = NULL;
pch = strrchr(fileName, '.');
if((NULL == pch) || (fileName == pch))
{
printf("Nope\n");
}
else
{
// Copy ot the extension
char fileextension[15];
strncpy(fileextension, (pch+1),15);
strcpy(fileExt, ".");
strcat(fileExt, fileextension);

// Remove the extension formthe filename
*pch = '\0';
strncpy(updateFileName, fileName, (METADATA_STR_MAX-1));
updateFileName[METADATA_STR_MAX-1] = 0;
}
}

int main()
{
meta_data_t test1 = {};
meta_data_t test2 = {};
meta_data_t test3 = {};
char tempfilename[METADATA_STR_MAX];

strncpy(test1.filename, "Poker Face.mp3", (METADATA_STR_MAX-1));
test1.filename[METADATA_STR_MAX-1] = 0;

strncpy(test2.filename, "Love.is.in.the.air.wma is the ext", (METADATA_STR_MAX-1));
test2.filename[METADATA_STR_MAX-1] = 0;

strncpy(test3.filename, "Crazy.ThisisatesttoCheckLength", (METADATA_STR_MAX-1));
test3.filename[METADATA_STR_MAX-1] = 0;

printf("%s \n",test1.filename);
printf("%s \n",test2.filename);
printf("%s \n\n",test3.filename);

strncpy(tempfilename, test1.filename, (METADATA_STR_MAX-1));
tempfilename[METADATA_STR_MAX-1] = 0;
GetFileExtMetadata(tempfilename, test1.fileextension, test1.filename);

printf(" BEFORE %s \n",test2.filename);
strncpy(tempfilename, test2.filename, (METADATA_STR_MAX-1));
tempfilename[METADATA_STR_MAX-1] = 0;
GetFileExtMetadata(tempfilename, test2.fileextension, test2.filename);
printf(" AFTER1 %s \n",test2.filename);

strncpy(tempfilename, test3.filename, (METADATA_STR_MAX-1));
tempfilename[METADATA_STR_MAX-1] = 0;
GetFileExtMetadata(tempfilename, test3.fileextension, test3.filename);
printf(" AFTER2 %s \n",test2.filename); // NOT GOOD

printf("%s - %s\n",test1.filename, test1.fileextension);
printf("%s - %s\n",test2.filename, test2.fileextension);
printf("%s - %s\n",test3.filename, test3.fileextension);

printf("%s \n",test1.filename);
printf("%s \n",test2.filename);
printf("%s \n",test3.filename);

return 0;
}

结果: 扑克脸.mp3 Love.is.in.the.air.wma 是分机 Crazy.This istesttoCheckLength

 BEFORE Love.is.in.the.air.wma is the ext 
AFTER1 Love.is.in.the.air
AFTER2
Poker Face - .mp3
- .wma is the ext
Crazy - .ThisisatesttoCh
Poker Face

Crazy

我第三次运行该函数后,发现文件名 Love.is.in.the.air 变空。我无法理解为什么会发生这种情况。有什么线索吗?谢谢

最佳答案

保存文件扩展名的缓冲区是不是太小了?看起来有 16 个字符 (EXTENSION_STR_MAX),但“Love.is.in.the.air.wma”不止于此。所以你会遇到经典的缓冲区溢出。

关于c++ - 具有操作数据重置功能的字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31511561/

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