The main use of fprintf with strings is as follows:
char *str1, *str2, *str3; FILE *f; // ... f = fopen("abc.txt", "w"); fprintf(f, "%s, %s\n", str1, str2); fprintf(f, "more: %s\n", str3); fclose(f);
You can add multiple lines using multiple %s format specifiers, and you can use fprintf repeated calls to gradually create the file.
If you have C ++ std::string objects, you can use their c_str() method to get a const char* suitable for use with fprintf :
std::string str("abc"); fprintf(f, "%s\n", str.c_str());
sth
source share