取一行中后几个字符

Tody Guo 2008-11-05 10:56:29
如文件格式为:
current time at \\test 1-1-2009 1:1 PM
Command successful

上面的是一个文件里内容的格式,我想将1-1-2009 1:1 PM保存到一个数组当中,其中\\test这个会变化,也就是说第一行的长度不固定,请问该如何操作??
...全文
205 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-11-06
  • 打赏
  • 举报
回复
用sscanf挺好使的~!
Tody Guo 2008-11-06
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>
int main()
{
char* s="current time at \\test 10-1-2009 1:1 PM";
char* p;
p=strchr(s,'-');

while(*p!=' ')p--;

printf("%s\n",p);
getch();
return 0;
}


还是这段代码经典,呵呵!!!
sevenhu 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyu637 的回复:]
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str = "current time at \\test 1-1-2009 1:1 PM";

int i = str.find("\\");

int j = str.find(" ",i);


string finalstr = str.substr(j+1,str.length());

cout < < finalstr < < endl;
return 0;
}
[/Quote]

up~~~
sevenhu 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ren03 的回复:]
从行末开始算起不就可以了吗
[/Quote]

同意~`
太乙 2008-11-05
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s="current time at \\test 1-1-2009 1:1 PM";
char res[100];
sscanf(s.c_str(),"%*[^\\]\\%*[^ ] %[^\0]",res);
cout<<res;
return EXIT_SUCCESS;
}
leank 2008-11-05
  • 打赏
  • 举报
回复
如果按LS说的,LZ的日期时间保存格式是有问题的,需要改成标通用格式 01-01-2009 01:01 PM
  • 打赏
  • 举报
回复
给你个思想:
你想保存的内容是日期和时间,这个是固定的长度,虽然\\test的会变化,但是我们可以计算机每行字符的长度,将行的总字符数减去日期和时间的长度,就是前面会变化的部分长度,之后用文件指针定位到这部分的末尾不就可以了吗?
ren03 2008-11-05
  • 打赏
  • 举报
回复
从行末开始算起不就可以了吗
wuyu637 2008-11-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str = "current time at \\test 1-1-2009 1:1 PM";

int i = str.find("\\");

int j = str.find(" ",i);


string finalstr = str.substr(j+1,str.length());

cout << finalstr << endl;
return 0;
}
sys0004 2008-11-05
  • 打赏
  • 举报
回复

// 先找第一个空格,然后拷贝后面的字符串到缓存区中
TCHAR *pStr = _T("\\test 1-1-2009 1:1 PM");
TCHAR szDate[32] = { 0 };
TCHAR *p = _tcschr(pStr, _T(' '));
if (p && ++p)
{
_tcscpy(szDate, p);
}

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧