I have string str = "6.5.1"
I want to write str to file .txt, but the result is ��j
Here my code
FILE *outfile = fopen("solution.txt", "w");
string test = "6.5.1";
fprintf(outfile, "%s\n", test);
I use string, FILE because I want to pass the file as an argument, and convert string from another file to method.
How can I fix this problem?
Thanks.
p\s: sorry, tag is c++ not c
>Solution :
You clarified that you needed a c++ solution and required to use FILE *:
#include <cstdio>
#include <string>
int main(void) {
FILE *outfile = std::fopen("solution.txt", "w");
std::string test = "6.5.1";
std::fprintf(outfile, "%s\n", test.c_str());
std::fclose(outfile);
}