C ++ regarding fprintf and ofstream

I used fprintf for a while and I would like to ask a question. Which is equivalent to this line fprintf :

 fprintf(OutputFile, "%s", "SomeStringValue"); 

using ofstream ?

How to use the "% s" in ofstream - this is what I really wanted to know. How to accept the following argument and print it as a string?

+4
source share
2 answers

You do not use it.

Essentially equivalent:

 std::ofstream x("your_file"); x << "SomeStringValue"; x.close(); 

Read about it on any of several help pages. For example http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

+3
source

You can not. You just write a string to the stream.

If you want to specify some additional line formatting (for example, right alignment with a space), you can use the setfill(' ') I / O setfill(' ') (specify a fill character with a space character) and setw(length) (set the output width). If you want something that mimics the syntax of C style style strings, you can use Boost.format .

 std::cout << boost::format("%s") % "SomeStringValue"; 
+3
source

Source: https://habr.com/ru/post/1497346/


All Articles