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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:15:10+00:00 2026-06-13T12:15:10+00:00

We have a large amount of demo code that uses sprintf and cstrings to

  • 0

We have a large amount of “demo” code that uses sprintf and cstrings to construct filenames. I’d like to replace it with c++ strings to allow very long filenames and because it gives a cleaner syntax.

So essentially we need to convert blocks like

ofstream some_file;
char filename[100];
sprintf(filename,"%s/soln%i.dat",a.c_str(), b);
some_file.open(filename);

into something like

ofstream some_file((a + "soln" + to_string(b) + ".dat").c_str());

However because this is used in a lot of places I’d like to use some kind of automatic conversion (i.e. sed expressions, emacs function/macros, visual studio?, etc.). The difficulties (that I can see) are:

  1. The contents of the sprintf statement can be anything. (So we need to parse an sprintf statement?)

  2. some_file and filename could be declared anywhere, could be named anything and often are reused for output to multiple files. (So we need to parse C++?)

Is this possible/feasible?

For bonus points can we avoid having to call .c_str() on the string (without changing the ofstream constructor)?

  • 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-13T12:15:11+00:00Added an answer on June 13, 2026 at 12:15 pm

    Here’s something simpler, w/o Yassnippet:

    (defun sprintf-to-ofstream (file)
      (interactive "sFile to print to: ")
      (back-to-indentation)
      (let* ((start (point))
             arglist
             sprintf-args
             ofstream-args
             (end 
              (save-excursion
                (move-end-of-line 1)
                (point)))
             (line (buffer-substring-no-properties start end)))
        (unless (string= (substring line 0 7) "sprintf")
          (error "No `sprintf' at this line"))
        (setq arglist
              (split-string
               (substring line (1+ (position ?\( line))
                          (position ?\) line :from-end t))
               "\\s-*,\\s-*")
              sprintf-args
              (split-string (substring (cadr arglist) 1 -1)
                            "%[^[:alpha:]%#]*[[:alpha:]%#]")
              ofstream-args
              (with-output-to-string
                (princ (concat "\"" (car sprintf-args) "\""))
                (setq sprintf-args (cdr sprintf-args))
                (dotimes (i (length sprintf-args))
                  (princ " + ")
                  (when (< (+ i 2) (length arglist))
                    (princ (nth (+ i 2) arglist))
                    (princ " + "))
                  (princ (concat "\"" (nth i sprintf-args) "\"")))))
        (kill-region start end)
        (insert (concat "ofstream " file "((" ofstream-args ").c_str());" ))))
    

    You could bind it to any key you like and use like so:

    • Move the point to the line containing sprintf you want to replace, for example by doing M-%sprintf, then call this function. It will prompt you to give it the name of the file where you want to print the message (probably I could make it more complex, and search upwards for the place where ofstream was declared, but if you say it may have been declared in an arbitrary place, that sounds like a wasted effort).
    • Press RET, after this the function will change the line to looks something like:
    ofstream foo(("" + a.c_str() + "/soln" + b + ".dat").c_str());
    

    given your original example. Obviously, if you think that adding a to_string() around arguments is more likely to be the case, then it is easy to do, given some more effort it will be also easy to avoid concatenating empty strings too, but some times it may change the effect / interpretation of the + operator, so I decided to keep it like this. I’ll try to make it into a yassnippet script a bit later today, so that it would be possible to jump to uncertain places in the replaced string interactively and have some pre-defined replacements too.

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

Sidebar

Related Questions

We have a large amount of C /C++ code that's compiled for multiple targets,
I have a large amount of python code that tries to handle numbers with
We have a large amount of legacy C++ code in shared libraries that are
I have a rather large amount of data (100 MB or so), that I
I have a WCF service that can return large amount of data depending on
I have a windows service that receives a large amount of data that needs
I have a large amount of bugs from Jira and Github that I want
I have a large amount of text data that I want to display in
I have a large amount of static data that needs to offer random access.
I have a large amount of log data that I need to get some

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.