0

I have to write a code for delaying subtitles.

I have to open .srt file change all the time for some time and save it in another file. I know how to open file and copy all to other file.

For example if i open "Subtitle.srt" and then write the name of output file "output" i would get the copied content of "Subtitle.srt" to "outputSubtitle.srt".

This is ok but I dont know how to delay time for instance if I input "10"

Original Subbtitle.srt 00:00:01,067 --> 00:00:03,963 and then I enter 10 outputSubtitle.srt 00:00:11,067 --> 00:00:13,963

I have to change all the times.

#include "stdafx.h"
#include "iostream"
#include "cstdlib"
#include "fstream"
#include "string"

 using namespace std;

 int main(int argc, char *argv[]){
 ifstream input; //input

 char input_file[32]; //names of input and output


  cout << "Enter name of input fille: "; ///user gives names of input
  cin >> input_file;

 input.open(input_file);
 if (!input.good()){
cout << "File " << input_file << " dosen't exist." << endl;
return 1;
}


string row;
while (!input.eof()){    
getline(input, row);

cout << row << endl;
}

system("pause");
return 0;
}
user3127680
  • 343
  • 2
  • 4
  • 12
  • Sorry but what do subtitles have to do with this code? – suspectus Jan 18 '14 at 21:02
  • So are the times in that xx:xx:xx,xxx format? – BWG Jan 18 '14 at 21:08
  • You're coding in C++ not C, so take advantage of it's features. Use `std::string` to avoid dangerous potential buffer overflow issues e.g. if the user types a filename bigger than 31 characters (which by the way, is a totally unreasonable number - modern OS's typically allow much longer filenames, and ones that don't are typically much less). Also, your use of `eof` [is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), and `system("pause")` [should be avoided](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong). – JBentley Jan 18 '14 at 21:36

1 Answers1

0

I'm assuming the string format is hour:minute:second,millisecond. Well, this function takes a string as an initial time, and an int amount to add in seconds. It probably breaks if you give it a negative time, but any reasonable positive time works just fine. It returns the output string, so you can juts print that where the old string was.

string add(string str, int amount) {
    int vals[8];
    sscanf(str.c_str(), "%u:%u:%u,%u --> %u:%u:%u,%u",
    &vals[0], &vals[1], &vals[2], &vals[3],
    &vals[4], &vals[5], &vals[6], &vals[7]);

    vals[2] += amount;
    vals[6] += amount;
    while((vals[2]>=60) || (vals[1]>=60) ||
          (vals[6]>=60) || (vals[5]>=60)) {
        if(vals[2] >= 60) {
            vals[2] -= 60;
            vals[1]++;
        }
        if(vals[6] >= 60) {
            vals[6] -= 60;
            vals[5]++;
        }
        if(vals[1] >= 60) {
            vals[1] -= 60;
            vals[0]++;
        }
        if(vals[5] >= 60) {
            vals[5] -= 60;
            vals[4]++;
        }
    }
    string out;
    out.resize(str.length());
    int n = sprintf(&out[0], "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u", 
    vals[0], vals[1], vals[2], vals[3],
    vals[4], vals[5], vals[6], vals[7]);

    return out;
};
BWG
  • 2,128
  • 1
  • 16
  • 29