字符串处理 、格式转换 。。。 求好的方法

噢噢噢噢 2011-04-17 01:34:58
原格式[]内的是毫秒 现在想把[]内的数字替换成 标准的lrc格式的 分:秒:毫秒
原格式:

[ar:Michael Jackson]
[ti:Beat it]
[1090] Beat It 避开!
[3230] 谨以此歌告诫年轻的朋友避免不必要的麻烦
[8650] 暴力不能解决一切问题,只会伤害到自己
[12770] 制作人:河洲
[20610] 1st Verse They Told Him 他们告诉他:   
[40160] Don't You Ever Come Around Here “你胆敢再来?   
[42720] Don't Wanna See Your Face You Better Disappear 你最好消失!
[45840] The Fire's In Their Eyes 怒火在他们眼中升腾
[48340] And Their Words Are Really Clear 话语也说得格外明白


目的格式:

[ar:Michael Jackson]
[ti:Beat it]
[00:01.09]Beat It 避开!
[00:03.23]谨以此歌告诫年轻的朋友避免不必要的麻烦
[00:08.65]暴力不能解决一切问题,只会伤害到自己
[00:12.77]制作人:河洲
[00:20.61]1st Verse] They Told Him 他们告诉他:   
[00:40.16]Don't You Ever Come Around Here “你胆敢再来?   
[00:42.72]Don't Wanna See Your Face, 不想再见你,   
[00:43.98]You Better Disappear 你最好消失!
[00:45.84]The Fire's In Their Eyes 怒火在他们眼中升腾
[00:48.34]And Their Words Are Really Clear 话语也说得格外明白
...全文
172 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
噢噢噢噢 2011-04-17
  • 打赏
  • 举报
回复
以解决,结贴给分走人
CppCoder 2011-04-17
  • 打赏
  • 举报
回复
把[]中的内容读的一个变量,处理变量,把结果放回[]中
太乙 2011-04-17
  • 打赏
  • 举报
回复
用什么不重要,你windows下一样编译运行[Quote=引用 11 楼 ilyysys 的回复:]

楼上用的linux?
[/Quote]
太乙 2011-04-17
  • 打赏
  • 举报
回复
$ g++ test.cpp //这里编译
$ ./a.out txt new_txt//这里运行
$ cat new_txt //查看运行后的结果输出文件
[ar:Michael Jackson]
[ti:Beat it]
[00:01.09] Beat It 避开!
[00:03.23] 谨以此歌告诫年轻的朋友避免不必要的麻烦
[00:08.65] 暴力不能解决一切问题,只会伤害到自己
[00:12.77] 制作人:河洲
[00:20.61] 1st Verse They Told Him 他们告诉他:   
[00:40.16] Don't You Ever Come Around Here “你胆敢再来?   
[00:42.72] Don't Wanna See Your Face You Better Disappear 你最好消失!
[00:45.84] The Fire's In Their Eyes 怒火在他们眼中升腾
[00:48.34] And Their Words Are Really Clear 话语也说得格外明白
噢噢噢噢 2011-04-17
  • 打赏
  • 举报
回复
楼上用的linux?
太乙 2011-04-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string format(int num)
{
num = num/10;
int i = num%100;
num = num/100;
int j = num%100;
num = num/100;
int k = num%100;
char buf[100];
sprintf(buf, "%02d:%02d.%02d", k, j, i);
return buf;
}
int main(int argc, char* argv[])
{
ifstream ifs(argv[1]);
ofstream ofs(argv[2]);
string line;
char buf[1024];
int num;
while (getline(ifs, line))
{
if (sscanf(line.c_str(), "[%d]%[^\n]", &num, buf) != 2)//这个地方改下
{
ofs << line << endl;
}
else
{
ofs << "[" << format(num) << "]" << buf << endl;
}
}
return 0;
}
太乙 2011-04-17
  • 打赏
  • 举报
回复
~/test$ cat txt
[ar:Michael Jackson]
[ti:Beat it]
[1090] Beat It 避开!
[3230] 谨以此歌告诫年轻的朋友避免不必要的麻烦
[8650] 暴力不能解决一切问题,只会伤害到自己
[12770] 制作人:河洲
[20610] 1st Verse They Told Him 他们告诉他:   
[40160] Don't You Ever Come Around Here “你胆敢再来?   
[42720] Don't Wanna See Your Face You Better Disappear 你最好消失!
[45840] The Fire's In Their Eyes 怒火在他们眼中升腾
[48340] And Their Words Are Really Clear 话语也说得格外明白
~/test$ ./a.out txt new_txt
~/test$ cat new_txt
[ar:Michael Jackson]
[ti:Beat it]
[00:01.09]Beat
[00:03.23]谨以此歌告诫年轻的朋友避免不必要的麻烦
[00:08.65]暴力不能解决一切问题,只会伤害到自己
[00:12.77]制作人:河洲
[00:20.61]1st
[00:40.16]Don't
[00:42.72]Don't
[00:45.84]The
[00:48.34]And
stain_less 2011-04-17
  • 打赏
  • 举报
回复
只能自己写一个方法来转换,很简单吧。
太乙 2011-04-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string format(int num)
{
num = num/10;
int i = num%100;
num = num/100;
int j = num%100;
num = num/100;
int k = num%100;
char buf[100];
sprintf(buf, "%02d:%02d.%02d", k, j, i);
return buf;
}
int main(int argc, char* argv[])
{
ifstream ifs(argv[1]);
ofstream ofs(argv[2]);
string line;
char buf[1024];
int num;
while (getline(ifs, line))
{
if (sscanf(line.c_str(), "[%d]%s", &num, buf) != 2)
{
ofs << line << endl;
}
else
{
ofs << "[" << format(num) << "]" << buf << endl;
}
}
return 0;
}
噢噢噢噢 2011-04-17
  • 打赏
  • 举报
回复
谢谢呵。。期待牛人出现
luciferisnotsatan 2011-04-17
  • 打赏
  • 举报
回复
读一行fgets
然后用sscanf,“[%d]”获取秒数。
接下来转换
噢噢噢噢 2011-04-17
  • 打赏
  • 举报
回复
正则表达式啊
bdmh 2011-04-17
  • 打赏
  • 举报
回复
有什么办法,先要读出[]中的数字,然后自己换算吗
噢噢噢噢 2011-04-17
  • 打赏
  • 举报
回复
有满意答案 分全给你

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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