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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:41:28+00:00 2026-06-18T13:41:28+00:00

I am writing strings to a text file using: ofstream ofs(ofs.txt, ios_base::binary); std::string str

  • 0

I am writing strings to a text file using:

ofstream ofs("ofs.txt", ios_base::binary);
std::string str = "Hi";
for (int i = 0 ; i < 10000000 ; i++) {
    ofs << str.c_str();
    ofs << "\n" ;
}

However, this takes long time to execute. Can anybody help me how to improve performance of the above. Or any other faster way to writing strings to file.

Thanks.

  • 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-06-18T13:41:28+00:00Added an answer on June 18, 2026 at 1:41 pm

    In several cases I found that C++ I/O streams tend to be slower than C <stdio.h> FILE*.

    I had a confirmation also in the following test:

    #define _CRT_SECURE_NO_WARNINGS // for stupid fopen_s warning
    #include <stdio.h>
    #include <exception>
    #include <fstream>
    #include <iostream>
    #include <ostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include <windows.h>
    using namespace std;
    
    long long Counter() 
    {
      LARGE_INTEGER li;
      QueryPerformanceCounter(&li);
      return li.QuadPart;
    }
    
    long long Frequency() 
    {
      LARGE_INTEGER li;
      QueryPerformanceFrequency(&li);
      return li.QuadPart;
    }
    
    void PrintTime(long long start, long long finish, const char * s) 
    {
      cout << s << ": " << (finish - start) * 1000.0 / Frequency() << " ms" << endl;
    }
    
    // RAII wrapper to FILE*
    class File
    {
    public:
      explicit File(FILE * f)
        : m_file(f)
      {}
    
      ~File()
      {
        fclose(m_file);
      }
    
      FILE* Get() const
      {
        return m_file;
      }
    
      bool IsOpen() const
      {
        return (m_file != nullptr);
      }
    
    private:
      FILE* m_file;
    
      File(const File&);
      File& operator=(const File&);
    };
    
    void TestIoStream(const vector<string>& lines)
    {
      ofstream ofs("ofs.txt", ios_base::binary);
      for(auto it = lines.begin(); it != lines.end(); ++it)
      {
        ofs << it->c_str();
        ofs << "\n" ;
      }
    }
    
    void TestStdioFile(const vector<string>& lines)
    {
      File file( fopen("cfile.txt", "wt") );
      if (! file.IsOpen())
        throw runtime_error("Can't open C FILE*.");
    
      for(auto it = lines.begin(); it != lines.end(); ++it)
      {
        fputs( it->c_str(), file.Get());
        fputs( "\n", file.Get());
      }
    }
    
    int main()
    {
      static const int kExitOk = 0;
      static const int kExitError = 1;
      try
      {
        cout << "Building test lines...";
        vector<string> lines;
        for (int i = 0; i < 10000000; i++)
          lines.push_back("Hi");
        cout << "done. ";
        cout << "(Count = " << lines.size() << ")" << endl;
    
        long long start = 0;
        long long finish = 0;
    
        start = Counter();  
        TestIoStream(lines);
        finish = Counter();
        PrintTime(start, finish, "C++ I/O stream");
    
        start = Counter();
        TestStdioFile(lines);
        finish = Counter();
        PrintTime(start, finish, "C FILE*");
    
        return kExitOk;
      }
      catch(const exception& e)
      {
        cerr << "\n*** ERROR: " << e.what() << endl;
        return kExitError;
      }
    }
    

    Compiled with VS2010 SP1 (VC10):

    cl /EHsc /W4 /nologo /MT /O2 /GL test.cpp
    

    Test result:

    Building test lines...done. (Count = 10000000)
    C++ I/O stream: 2892.39 ms
    C FILE*: 2131.09 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my logs in a text file at my local location(C:\temp\log.txt) using
I'm writing some strings to a file using the following function... void writeText(const char*
I'm using this function to read text lines from a file: string[] postFileLines =
Hey Im writing to a text file using file i/o using an array of
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
I am writing a program in Ruby which will search for strings in text
I'm writing a simple program to convert text to strings. One quick question, how
I am writing some code to type strings using the Robot class. Everything is
I'm writing a program that reads a text file of city names into a
Well, I'm writing a file of extensions/method useful to strings,label,linklabels,class etc. but, I have

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.