用C语言写的一个文本文件的批量行处理

有首歌的歌词一直很好奇,演唱的瑞典姑娘英文咬字不太清楚,只能听个半懂,但是用Google搜歌词屡搜屡败,只能找到一些人的听写,手头也只有iTunes买来的音源,又不舍得专门买一张CD看BK,最近发现N站的弹幕里有人打了出来,抽出COMMENT之后每行开头都有一部分不需要的时间轴,懒得专门下个软件处理了,干脆自己写试试,运行之后发现效果还不错,好在歌词是英文,处理方便

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char str[200];
 6     int i, len;
 7     FILE *ptr = fopen("lyric.txt","r");
 8     FILE *ptr2 = fopen("out.txt","w");
 9     while( !feof(ptr) )
10     {
11         fgets(str, 200 , ptr);
12         len = strlen(str);
13         for(i = 40; i < len; i++ )
14             fprintf(ptr2, "%c",str[i]);
15     }
16     
17     fclose(ptr);
18     fclose(ptr2);
19     
20     return 0;
21 }

源文件是这样的

53     00:01     05/02 07:16:26     SE_vPX     [↓] I am sure about it
54     00:03     05/02 07:16:58     SE_vPX     [↓] Now you have to know
55     00:05     05/02 07:17:08     SE_vPX     [↓] I'm gonna tell you 'bout what I've been hiding
56     00:09     05/02 07:18:08     SE_vPX     [↓] I am sure about it
57     00:11     05/02 07:18:18     SE_vPX     [↓] I will let you go
58     00:13     05/02 07:18:28     SE_vPX     [↓] And your melody will never find me
59     00:24     05/02 07:18:52     SE_vPX     [↓] Guess I was too scared of time
60     00:30     05/02 07:19:23     SE_vPX     [↓] I'm waiting for those days for sure
61     00:38     05/02 07:19:54     SE_vPX     [↓] Guess I was too slow to run
62     00:45     05/02 07:20:21     SE_vPX     [↓] That second time,
63     00:48     05/02 07:20:33     SE_vPX     [↓] wasted all my life
64     00:53     05/02 07:21:02     SE_vPX     [↓] Now that I've found these things to say 'bout my home
65     01:00     05/02 07:21:19     SE_vPX     [↓] Now that know what makes you safe
66     01:06     05/02 07:21:39     SE_vPX     [↓] I am sure about it
67     01:07     05/02 07:21:49     SE_vPX     [↓] Now you have to know
68     01:09     05/02 07:21:59     SE_vPX     [↓] I'm gonna tell you 'bout what I've been hiding
69     01:13     05/02 07:22:28     SE_vPX     [↓] I am sure about it
70     01:15     05/02 07:22:37     SE_vPX     [↓] I will let you go
71     01:17     05/02 07:23:03     SE_vPX     [↓] Now your melody will never find me
72     01:20     05/02 07:25:11     SE_vPX     [↓] I am sure about it
73     01:22     05/02 07:25:20     SE_vPX     [↓] Now you have to know
74     01:24     05/02 07:25:32     SE_vPX     [↓] I'm gonna tell you 'bout what I've been hiding
75     01:28     05/02 07:25:43     SE_vPX     [↓] I am sure about it
76     01:30     05/02 07:25:52     SE_vPX     [↓] I will let you go
77     01:31     05/02 07:26:04     SE_vPX     [↓] And your melody will never find me
78     01:37     05/02 07:26:16     SE_vPX     [↓] I will tell you all
79     01:54     05/02 07:27:07     SE_vPX     [↓] I made all mistakes at once
80     02:00     05/02 07:27:21     SE_vPX     [↓] Like catching all those nails I caught
81     02:08     05/02 07:28:53     SE_vPX     [↓] You wept 'til your eyes were dry
82     02:15     05/02 07:29:09     SE_vPX     [↓] You couldn't see
83     02:19     05/02 07:29:17     SE_vPX     [↓] those tears of doubt
84     02:30     05/02 07:30:44     SE_vPX     [↓] Now
85     02:31     05/02 07:30:57     SE_vPX     [↓]【うp主が削除】 that I've found these things to say 'bout my home
86     02:22     05/02 07:34:29     SE_vPX     [↓] Now
87     02:23     05/02 07:34:44     SE_vPX     [↓] that I've found these things to say 'bout my home
88     02:31     05/02 07:34:57     SE_vPX     [↓] that know what makes you safe
89     02:36     05/02 07:35:07     SE_vPX     [↓] I am sure about it
90     02:37     05/02 07:35:18     SE_vPX     [↓] Now you have to know
91     02:39     05/02 07:35:44     SE_vPX     [↓] I'm gonna tell you 'bout what
92     02:41     05/02 07:35:55     SE_vPX     [↓] I've been hiding
93     02:43     05/02 07:36:06     SE_vPX     [↓] I am sure about it
94     02:45     05/02 07:36:16     SE_vPX     [↓] I will let you go
95     02:47     05/02 07:36:26     SE_vPX     [↓] Now your melody will never find me
96     02:50     05/02 07:36:37     SE_vPX     [↓] I am sure about it
97     02:52     05/02 07:36:45     SE_vPX     [↓] Now you have to know
98     02:54     05/02 07:36:58     SE_vPX     [↓] I'm gonna tell you 'bout what
99     02:56     05/02 07:37:05     SE_vPX     [↓] I've been hiding
100     02:58     05/02 07:37:14     SE_vPX     [↓] I am sure about it
101     02:59     05/02 07:37:20     SE_vPX     [↓] I will let you go
102     03:01     05/02 07:37:47     SE_vPX     [↓] And your melody will
103     03:03     05/02 07:37:59     SE_vPX     [↓] never find me
104     03:06     05/02 07:38:41     SE_vPX     [↓] I will tell you all
105     03:38     05/02 07:39:29     SE_vPX     [↓] Guess I was telling
106     03:14     05/02 07:39:47     SE_vPX     [↓] I will tell you all
107     03:41     05/02 07:40:06     SE_vPX     [↓] my story today
108     03:21     05/02 07:40:32     SE_vPX     [↓] I will tell you all
109     03:45     05/02 07:41:07     SE_vPX     [↓] Guess you were listening
110     03:48     05/02 07:41:17     SE_vPX     [↓] to something I said
111     03:29     05/02 07:41:51     SE_vPX     [↓] I will tell you all

输出文件是这样的,只有日文的部分乱码了,以及编号超过100时对不齐多读了一个空格,其他的都达到了预期的要求

I am sure about it
Now you have to know
I'm gonna tell you 'bout what I've been hiding
I am sure about it
I will let you go
And your melody will never find me
Guess I was too scared of time
I'm waiting for those days for sure
Guess I was too slow to run
That second time,
wasted all my life
Now that I've found these things to say 'bout my home
Now that know what makes you safe
I am sure about it
Now you have to know
I'm gonna tell you 'bout what I've been hiding
I am sure about it
I will let you go
Now your melody will never find me
I am sure about it
Now you have to know
I'm gonna tell you 'bout what I've been hiding
I am sure about it
I will let you go
And your melody will never find me
I will tell you all
I made all mistakes at once
Like catching all those nails I caught
You wept 'til your eyes were dry
You couldn't see
those tears of doubt
Now
兢Γ鹬鳏鞒?that I've found these things to say 'bout my home
Now
that I've found these things to say 'bout my home
that know what makes you safe
I am sure about it
Now you have to know
I'm gonna tell you 'bout what
I've been hiding
I am sure about it
I will let you go
Now your melody will never find me
I am sure about it
Now you have to know
I'm gonna tell you 'bout what
I've been hiding
 I am sure about it
 I will let you go
 And your melody will
 never find me
 I will tell you all
 Guess I was telling
 I will tell you all
 my story today
 I will tell you all
 Guess you were listening
 to something I said
 I will tell you all

最近泡坛子的时候看到有人谈论用正则表达式对文本进行处理,挺感兴趣,有空研究一下

原文地址:https://www.cnblogs.com/joyeecheung/p/2913873.html