Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 487375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:35:54+00:00 2026-05-13T01:35:54+00:00

I’m having trouble extracting the timings from a .srt (subtitle) file and writing it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:35:55+00:00Added an answer on May 13, 2026 at 1:35 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.