从多维数组中提取时序并写入c中的文件

发布于 2024-08-13 15:11:58 字数 1359 浏览 1 评论 0原文

我无法从 .srt (字幕)文件中提取时间并将其写入另一个名为output.srt的文件。当我运行以下命令时,我在输出文件中写入了一些有趣的内容。

// 其中 hr=小时,mn=分钟,sc=秒,ms=mili 秒

#include <stdio.h>
#define LINES 50
#define CHARAC 80
int main(void){
    FILE *in;
    FILE *out;
    char text[LINES][CHARAC];
    char timings[LINES][CHARAC];
    int i=0,lines=0,items=0;
    int hr=0,mn=0,sc=0,ms=0,hr2=0,mn2=0,sc2=0,ms2=0;

    in=fopen("file2.srt","r");
    out=fopen("output.srt","w");

    while (!feof(in)){
        fgets(text[i],80,in);
        items=sscanf(text[i],"%d:%d:%d,%d --> %d:%d:%d,%d ",&hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
        //------------------------------------->edited<----------------------------------
        switch (items)
        {   
            case 1: break;
            case 8: 
                sprintf(timings[i],"%d:%d:%d,%d --> %d:%d:%d,%d",hr,mn,sc,ms,hr2,mn2,sc2,ms2);
                break;
            case 0: break;

        }
        //------------------------------------->edited<----------------------------------
        ++i;
    }
    lines=i;

    for (int i=0;i<lines;i++){
        fprintf(out,"%s\n",timings[i]);
    }

    fclose(in);
    fclose(out);

    return 0;
}

我如何提取前 10 个计时?

I'm having trouble extracting the timings from a .srt (subtitle) file and writing it to another file called output.srt. When i run the following i get some funky stuff written onto the output file.

// where hr=hours,mn=minutes,sc=seconds,ms=mili seconds

#include <stdio.h>
#define LINES 50
#define CHARAC 80
int main(void){
    FILE *in;
    FILE *out;
    char text[LINES][CHARAC];
    char timings[LINES][CHARAC];
    int i=0,lines=0,items=0;
    int hr=0,mn=0,sc=0,ms=0,hr2=0,mn2=0,sc2=0,ms2=0;

    in=fopen("file2.srt","r");
    out=fopen("output.srt","w");

    while (!feof(in)){
        fgets(text[i],80,in);
        items=sscanf(text[i],"%d:%d:%d,%d --> %d:%d:%d,%d ",&hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
        //------------------------------------->edited<----------------------------------
        switch (items)
        {   
            case 1: break;
            case 8: 
                sprintf(timings[i],"%d:%d:%d,%d --> %d:%d:%d,%d",hr,mn,sc,ms,hr2,mn2,sc2,ms2);
                break;
            case 0: break;

        }
        //------------------------------------->edited<----------------------------------
        ++i;
    }
    lines=i;

    for (int i=0;i<lines;i++){
        fprintf(out,"%s\n",timings[i]);
    }

    fclose(in);
    fclose(out);

    return 0;
}

how do I go about extracting those first 10 timings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-08-20 15:11:58

如果这是在 Windows(或 MSDOS)上,则打开模式需要是文本:

in =  fopen ("file2.srt", "rt");
out = fopen ("output.srt", "wt");

其次,代码不会对不同格式的行做出任何反应。前几行数据是:

1
00:00:30,909--> 00:00:32,775
Take a look at yourself.

2
00:00:34,066--> 00:00:37,681
Disconnect you from the seats,
lift yourself and take a look in the mirror.

因此,自然地,第一个 sscanf 不会填充大部分字段。这是我得到的输出(针对相应的行):

1:0:0,0 --> 0:0:0,0
0:0:30,909 --> 0:0:32,775
0:0:30,909 --> 0:0:32,775
0:0:30,909 --> 0:0:32,775
2:0:30,909 --> 0:0:32,775

要解决此问题,您必须添加需要正确数量的元素的逻辑,或者至少对它们做出反应:

itms = sscanf(text[i],"%d:%d:%d,%d --> %d:%d:%d,%d ",&hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
switch (itms)
{
case 1:  // the first line
case 8:  // the lines with the times
case 0:  // the text lines
}

编辑以添加最后一个的固定版本编辑:

#include <stdio.h>
#define LINES 50
#define CHARAC 80
int main(void){
    FILE *in;
    FILE *out;
    char text[LINES][CHARAC];
    char timings[LINES][CHARAC];
    int i=0,lines=0,items=0;
    int hr=0,mn=0,sc=0,ms=0,hr2=0,mn2=0,sc2=0,ms2=0;

    in=fopen("file2.srt","rt");
    out=fopen("output.srt","wt");

    while (!feof(in))
    {
        if (!fgets(text[i],80,in))
            break;
        items = sscanf(text[i], "%d:%d:%d,%d --> %d:%d:%d,%d ", &hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
        switch (items)
        {       
        case 1: break;
        case 8: 
                sprintf(timings[i],"%d:%d:%d,%d --> %d:%d:%d,%d",hr,mn,sc,ms,hr2,mn2,sc2,ms2);
                ++i;  // advance only when a valid line is seen
                break;
        case 0: break;
        }
    }
    lines=i;

    for (i=0; i<lines; i++){
        fprintf(out,"%s\n",timings[i]);
    }

    fclose(in);
    fclose(out);

    return 0;
}

If this is on windows (or MSDOS) the open modes need to be text:

in =  fopen ("file2.srt", "rt");
out = fopen ("output.srt", "wt");

Secondly, the code isn't doing anything to react to the differently formatted lines. The first few data lines are:

1
00:00:30,909--> 00:00:32,775
Take a look at yourself.

2
00:00:34,066--> 00:00:37,681
Disconnect you from the seats,
lift yourself and take a look in the mirror.

So, naturally, the first sscanf isn't going to fill in most of the fields. Here's the output I got (for the corresponding lines):

1:0:0,0 --> 0:0:0,0
0:0:30,909 --> 0:0:32,775
0:0:30,909 --> 0:0:32,775
0:0:30,909 --> 0:0:32,775
2:0:30,909 --> 0:0:32,775

To fix this, you'll have to add logic which expects the proper number of elements, or at least reacts to them:

itms = sscanf(text[i],"%d:%d:%d,%d --> %d:%d:%d,%d ",&hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
switch (itms)
{
case 1:  // the first line
case 8:  // the lines with the times
case 0:  // the text lines
}

Edited to add a fixed version of your last edit:

#include <stdio.h>
#define LINES 50
#define CHARAC 80
int main(void){
    FILE *in;
    FILE *out;
    char text[LINES][CHARAC];
    char timings[LINES][CHARAC];
    int i=0,lines=0,items=0;
    int hr=0,mn=0,sc=0,ms=0,hr2=0,mn2=0,sc2=0,ms2=0;

    in=fopen("file2.srt","rt");
    out=fopen("output.srt","wt");

    while (!feof(in))
    {
        if (!fgets(text[i],80,in))
            break;
        items = sscanf(text[i], "%d:%d:%d,%d --> %d:%d:%d,%d ", &hr,&mn,&sc,&ms,&hr2,&mn2,&sc2,&ms2);
        switch (items)
        {       
        case 1: break;
        case 8: 
                sprintf(timings[i],"%d:%d:%d,%d --> %d:%d:%d,%d",hr,mn,sc,ms,hr2,mn2,sc2,ms2);
                ++i;  // advance only when a valid line is seen
                break;
        case 0: break;
        }
    }
    lines=i;

    for (i=0; i<lines; i++){
        fprintf(out,"%s\n",timings[i]);
    }

    fclose(in);
    fclose(out);

    return 0;
}
一身仙ぐ女味 2024-08-20 15:11:58

我注意到的第一件事是您没有检查 sscanf() 的结果。您应该检查返回代码以确保所有八个项目都被扫描进来,并且如果数据被正确扫描,则只有 sprintf() 到timings[]。

The first thing I notice is that you're not checking the result of the sscanf(). You should check the return code to make sure all eight items were scanned in, and only sprintf() to timings[] if the data was scanned correctly.

森林很绿却致人迷途 2024-08-20 15:11:58

您需要检查 sscanf() 的返回值以查看该行是否匹配。我下载了您的示例文件:

1
00:00:30,909--> 00:00:32,775
Take a look at yourself.

2
00:00:34,066--> 00:00:37,681
Disconnect you from the seats,
lift yourself and take a look in the mirror.

只有其中一些行会从 sscanf() 返回 8 作为匹配数,因此请进行测试。当然,如果文本行也匹配的话,这将会失败!

更好的方法是查找空行(第一行除外),然后查找一行上的整数,然后查找时间。仅当三个都起作用时才匹配。

You need to check the return value from sscanf() to see if the line matched. I downloaded your sample file:

1
00:00:30,909--> 00:00:32,775
Take a look at yourself.

2
00:00:34,066--> 00:00:37,681
Disconnect you from the seats,
lift yourself and take a look in the mirror.

Only some of these lines will return 8 from sscanf() as the number of matches, so test for that. Of course, this will fail if text line matches that, too!

Better would be to look for a blank line (except for the first), then an integer on one line, then the timing. Match only if all three work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文