从给定日期提取日、月、年
Extracting day,month,year from a given date
我试图使用sscanf从给定日期中提取日、月、年,但似乎不起作用。这是我的一段代码。。。
我已将日期存储在一个字符数组中。
void dateinp(char date[])
{
char d[5];
char y[5];
char mm[5];
sscanf(date,"%s-%s-%s",d,mm,y);
printf("%sn%sn%sn",d,mm,y);
}
我哪里错了?
2015年1月12日输入时,我得到:2015年1月12日-2015年1月12日N-20155
您的代码位于C
中,而问题标记为C++
。这是一个C++
版本:
// accepts 12-JAN-2015
void dateinp(const string& date)
{
string d = date.substr(0, 2);
string mm = date.substr(3, 3);
string y = date.substr(7, 4);
cout << d << "/" << mm << "/" << y << endl;
}
另一个版本是:
#include <sstream>
#include <iostream>
using namespace std;
// accepts 12-JAN-2015, also accepts 2-JAN-2015 (where the day is just a single digit)
void dateinp(const string& date)
{
stringstream ss(date);
string d, mm, y;
getline(ss, d, '-');
getline(ss, mm, '-');
getline(ss, y, '-');
cout << d << "/" << mm << "/" << y << endl;
}
int day, month, year;
sscanf(buffer, "%2d/%2d/%4d",
&month,
&day,
&year);
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 如何打开并写入一个名称取自C++中字符串的文件
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- scanf() 只取数字作为输入,避免使用其他字符
- 如何从C++中的字符串ID中提取int日,月和年
- 从给定日期提取日、月、年
- 从c++中的std::chrono::time_point提取年/月/日等