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 7533723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:48:26+00:00 2026-05-30T05:48:26+00:00

I have subtitles /srt files with data being generated by the second. I am

  • 0

I have subtitles /srt files with data being generated by the second. I am trying to extract the data from them and save them to text files
in a while(1) loop, its a simple text read and write operation.

But i have found out using fgets to traverse through the file is causing this simple operation to have very high cpu usage ( top command ref). more the number of fgets higher the
cpu usage.

The code bit:

int convert_to_txt(FILE *f1, FILE *f2) {

    static int str_len = 0;
    char cap_nxt_tmp[128];

    while (fgets(cap_nxt_tmp, 128, f1) != NULL) {

        fprintf(f2, "%s\n", cap_nxt_tmp);
    }
    return 0;
}

int main(int argc, char* argv[]) {
    FILE *inputFilePtr;
    FILE *outputFilePtr;

    char inputPath[1024] = "storage/sample.srt";
    char outputPath[1024] = "storage/sample.txt";
    while (1) {

        outputFilePtr = fopen(outputPath, "w");
        inputFilePtr = fopen(inputPath, "r");
        if (inputFilePtr == NULL || outputFilePtr == NULL) {
            perror("Error");
        }
        convert_to_txt(inputFilePtr, outputFilePtr);
        fclose(inputFilePtr);
        fclose(outputFilePtr);
        // theres a break on an end condition lets say least run time is an hour.
    }
    return 0;
}

i cant understand whats wrong in the way i am using fgets to read the srt/captions file ( 1-2 mb per file), a simple text file i/o/read operation should not consume too much cpu usage.

this basic file i/o in a while(1) of a language like python shows only a 2-3% cpu usage.
but this in c is showing about 30% cpu usage.
How can i reduce the cpu usage without using sleep . is there any less cpu alternative to fgets?

  • 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-30T05:48:27+00:00Added an answer on May 30, 2026 at 5:48 am

    I don’t see anything wrong or strange here.

    Your main while(1) loop never stops, which means you’re converting your files over and over again. Files get cached by the OS, so you’re not really accessing your physical drives. The program therefore spends almost all the CPU time doing strlen(cap_nxt_tmp), and you observe very high CPU load.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subtitle text file that works with standart srt format 00:00:00,000 Hour,
I want to format srt subtitle text files to avoid wrapping problems on my
I have two subtitles files. I need a function that tells whether they represent
I have a flash app that plays movies. It loads subtitles and subtitle times
I have an XML like this: <EXP> <TITLES> <SUBTITLE CL=AXT4 FL=1 NB=Text 1/> </TITLES>
I have images that change every 5000ms and I wanted to put subtitles on
I'm trying to build a small app which allows downloading of multiple files at
Hello I am trying to pull information from a page on Wikia for The
Using C# how should I go about extracting titles subtitles and paragraphs from a
So I'm trying to have an effect that when you click on an image,

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.