gpt4 book ai didi

C++ 从一个字符中获取指定的部分

转载 作者:行者123 更新时间:2023-11-30 02:04:45 24 4
gpt4 key购买 nike

我从一个文本文件中读取一行,行尾有数字(格式:“some text.001”),我想获取 0 或 0 之后的数字。所以如果是001,那么就是1,如果是010,那么就是10。我现在得到的:

fgets(strLine, 100, m_FileStream);
// Here I need to cut the numbers into myNum
int num = atoi(&myNum);

我尝试使用 strrchr 获取“.”的位置,但不知道下一步是什么。也许我需要 strtok,但我不知道如何使用它。

最佳答案

一旦你得到了 . 的位置,你就可以前进一个 char 并使用 atoi():

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

int main()
{
char buf[20] = "some text.010";
char* period_ptr = strrchr(buf, '.');
if (period_ptr)
{
printf("%d\n", atoi(++period_ptr));
}
return 0;
}

关于C++ 从一个字符中获取指定的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10229844/

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