C/Java-读取2个文件,一次写入

C/Java-读取2个文件,一次写入,c,C,我需要字幕方面的帮助=) 我有两个带字幕的.srt文件。一个是英语,另一个是斯洛文尼亚语。问题是斯洛文尼亚文中的文件没有正确的 时间码,所以字幕比实际行快。我想做的是编写一个程序,读取两个文件,获取字幕和 eng.srt文件中的时间码和slo.srt文件中的字幕,并将所有内容写入complete.srt。我不在乎它是用Java还是C。 我目前正在尝试用C语言编写程序,如果有任何帮助,我将不胜感激 现在来演示我想做什么: eng.srt (right timecode) 1 00:00:01,2

我需要字幕方面的帮助=)

我有两个带字幕的.srt文件。一个是英语,另一个是斯洛文尼亚语。问题是斯洛文尼亚文中的文件没有正确的 时间码,所以字幕比实际行快。我想做的是编写一个程序,读取两个文件,获取字幕和 eng.srt文件中的时间码和slo.srt文件中的字幕,并将所有内容写入complete.srt。我不在乎它是用Java还是C。 我目前正在尝试用C语言编写程序,如果有任何帮助,我将不胜感激

现在来演示我想做什么:

eng.srt (right timecode)

1
00:00:01,259 --> 00:00:03,734
<i>Previously on...</i>

2
00:00:03,746 --> 00:00:06,910
<i>Tom and Lynette drifted further apart,</i>

3
00:00:06,911 --> 00:00:09,275
<i>and Jane took advantage.</i>

4
00:00:09,440 --> 00:00:10,670
I'm scared.

5
00:00:10,671 --> 00:00:13,362
<i>Roy helped Karen face her cancer.</i>




slo.srt (right subtitles)

1
00:00:00,009 --> 00:00:02,484
<i>Prejšnič...</i>

2
00:00:02,496 --> 00:00:05,660
<i>Tom and Lynette
sta se še bolj odtujila,</i>

3
00:00:05,661 --> 00:00:08,025
<i>in Jane je to izkoristila.</i>

4
00:00:08,190 --> 00:00:09,420
Strah me je.

5
00:00:09,421 --> 00:00:12,112
<i>Roy se je pomagal Karen
soočiti z rakom.</i>



complete.srt (where i write)

1
00:00:01,259 --> 00:00:03,734
<i>Prejšnič...</i>

2
00:00:03,746 --> 00:00:06,910
<i>Tom and Lynette
sta se še bolj odtujila,</i>
...
eng.srt(正确的时间码)
1.
00:00:01,259 --> 00:00:03,734
前情提要。。。
2.
00:00:03,746 --> 00:00:06,910
汤姆和勒奈特越来越疏远,
3.
00:00:06,911 --> 00:00:09,275
简占了便宜。
4.
00:00:09,440 --> 00:00:10,670
我很害怕。
5.
00:00:10,671 --> 00:00:13,362
罗伊帮助凯伦面对癌症。
slo.srt(右字幕)
1.
00:00:00,009 --> 00:00:02,484
普雷什尼奇。。。
2.
00:00:02,496 --> 00:00:05,660
汤姆和勒奈特
斯塔塞什·博尔杰·奥杜吉拉,
3.
00:00:05,661 --> 00:00:08,025
从简·杰到伊兹科里斯特拉。
4.
00:00:08,190 --> 00:00:09,420
给我来一杯。
5.
00:00:09,421 --> 00:00:12,112
Roy se je pomagal Karen
soočiti z rakom。
complete.srt(我写的地方)
1.
00:00:01,259 --> 00:00:03,734
普雷什尼奇。。。
2.
00:00:03,746 --> 00:00:06,910
汤姆和勒奈特
斯塔塞什·博尔杰·奥杜吉拉,
...
这是我到目前为止所拥有的(我打开了3个文件,我将边走边更新我的工作):

#包括
#包括
int main()
{
char-ch,sf1[20],sf2[20],tf[20];
文件*source1、*source2、*target;
//第一个源文件
printf(“输入第一个源文件的名称\n”);
获取(sf1);
source1=fopen(sf1,“r”);
//秒源文件
printf(“输入第二个源文件的名称\n”);
获取(sf2);
source2=fopen(sf2,“r”);
if(source==NULL)
{
printf(“按任意键退出…\n”);
退出(退出失败);
}
//目标文件
printf(“输入目标文件的名称”);
获取(tf);
目标=fopen(tf,“w”);
if(target==NULL)
{
fclose(来源);
printf(“按任意键退出…\n”);
退出(退出失败);
}
printf(“文件写入成功。\n”);
fclose(source1);
fclose(source2);
fclose(目标);
返回0;
}

我的问题是,我不知道如何告诉程序只读取eng.srt文件中的数字,然后跳过字幕部分并等待,然后读取slo.srt文件,取出字幕并跳过数字。

通过awk等模式匹配语言,这更容易做到。这里的模式非常简单。对于时间代码,它以两位数字(^[0-9][0-9])开头,副标题以(^)开头。我没有详细说明解决方案,因为我不知道您是否会使用这些脚本语言之一。

主要逻辑很简单。这是它的
伪代码

for each subtitle in file1 and file2:
    extract_time_from_file1;
    extract_subtitle_from_file2;
    write_into_new_file_combining_the_time_and_string
下面是一个完全有效的代码:

#include <iostream>
#include <fstream>
using namespace std;
string read_title_string(ifstream& in)
{
    string ans="";
    string tmp;
    getline(in, tmp);//neglect the subtitle number
    getline(in, tmp);//neglect the time..
    /*sub-title extraction*/
    while(1)//read until the blank line and store all the strings..
    {
     getline(in, tmp);
     if(tmp.length()==0)
        break;
    ans += tmp;
    }
    return ans;
}
string read_title_time(ifstream& in)
{
    string ans="";
    string tmp;
    getline(in, tmp);//ignore subtitle number..
    getline(in, ans);//this is what we want..
    while(1)//read until a blank line and ignore them..
    {
        getline(in, tmp);
        if(tmp.length()==0)
            break;
    }
    return ans;
}
int main()
{
    ifstream ins("slo.srt"),outs("eng.srt");
    ofstream ans("complete.srt");
    int count=1;
    while(!ins.eof() && !outs.eof())
    {
        ans<<count++<<endl;
        ans<<read_title_time(outs)<<endl;
        ans<<read_title_string(ins)<<endl;
        ans<<endl;
    }
    ins.close();outs.close();ans.close();
    return 0;
}
#包括
#包括
使用名称空间std;
字符串读取\标题\字符串(ifstream&in)
{
字符串ans=“”;
串tmp;
getline(in,tmp);//忽略字幕编号
getline(in,tmp);//忽略时间。。
/*子标题提取*/
(1)//读取直到空白行并存储所有字符串。
{
getline(in,tmp);
如果(tmp.length()==0)
打破
ans+=tmp;
}
返回ans;
}
字符串读取时间(ifstream&in)
{
字符串ans=“”;
串tmp;
getline(in,tmp);//忽略字幕编号。。
getline(in,ans);//这就是我们想要的。。
while(1)//读到一个空行并忽略它们。。
{
getline(in,tmp);
如果(tmp.length()==0)
打破
}
返回ans;
}
int main()
{
如果输入(“slo.srt”)、输出(“eng.srt”);
流ans(“complete.srt”);
整数计数=1;
而(!ins.eof()&&!outs.eof())
{

Ans请仔细阅读“帮助”部分。这不是简单地询问代码的地方。你尝试过什么吗?在询问堆栈溢出问题时,展示代码片段是一个很好的做法,你的问题/问题到底是什么?嗯,这段代码对我不起作用。它启动得很好,不会报告任何错误,但它不会在complete.srt文件中写入任何内容。仅使用3个副标题行尝试。我依赖于副标题文件的结构。如果其中至少有一个额外的空白,则会失败。请尝试改进此代码以获得准确的输出。
#include <iostream>
#include <fstream>
using namespace std;
string read_title_string(ifstream& in)
{
    string ans="";
    string tmp;
    getline(in, tmp);//neglect the subtitle number
    getline(in, tmp);//neglect the time..
    /*sub-title extraction*/
    while(1)//read until the blank line and store all the strings..
    {
     getline(in, tmp);
     if(tmp.length()==0)
        break;
    ans += tmp;
    }
    return ans;
}
string read_title_time(ifstream& in)
{
    string ans="";
    string tmp;
    getline(in, tmp);//ignore subtitle number..
    getline(in, ans);//this is what we want..
    while(1)//read until a blank line and ignore them..
    {
        getline(in, tmp);
        if(tmp.length()==0)
            break;
    }
    return ans;
}
int main()
{
    ifstream ins("slo.srt"),outs("eng.srt");
    ofstream ans("complete.srt");
    int count=1;
    while(!ins.eof() && !outs.eof())
    {
        ans<<count++<<endl;
        ans<<read_title_time(outs)<<endl;
        ans<<read_title_string(ins)<<endl;
        ans<<endl;
    }
    ins.close();outs.close();ans.close();
    return 0;
}