Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, regarding fprintf and ofstream

I've been using fprintf for a while now and I'd like to ask a question. What is the equivalent of this fprintf line:

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

using ofstream ?

How to use the "%s" in ofstream is what I'd really like to know. How to take the next argument and print it as a string?

like image 630
aresz Avatar asked Aug 15 '13 23:08

aresz


People also ask

What is fprintf () in C?

fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...);

What is the use of fprintf?

The fprintf function is used for printing information to the screen. The fprintf function prints an array of characters to the screen: fprintf('Happy Birthday\n'); We often use the fprintf statement to show the user information stored in our variables.

Is ofstream an Ostream?

ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.

What is the use of ifstream and ofstream in C++?

ifstream is input file stream which allows you to read the contents of a file. ofstream is output file stream which allows you to write contents to a file. fstream allows both reading from and writing to files by default.


1 Answers

You don't use it.

The equivalent is essentially:

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

Go read about it on any of several reference pages. Such as http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

like image 64
UpAndAdam Avatar answered Sep 28 '22 01:09

UpAndAdam