unexpected output from fprintf

I have the following piece of code which should write the contents of my vetor to y file but I am getting a very weird output in my file.

1
2
3
4
5
6
7
8
for (int i=0;i<amount;i++) {
	fprintf(pFileO,"Case #%d: ",i+1);
		for (int j=0;j<words[i].size();j++) {
			fprintf(pFileO,"%s",words[i][words[i].size()-j-1]);
			cout<<words[i][words[i].size()-j-1]<<endl;
		}
	fprintf(pFileO,"\n");
}


Output in cmd:

test
a
is
this
foobar
base
your
all
class
along
pony


output in my file:

Case #1: 0¤a¤að£aУa
Case #2: Ȭa
Case #3: <®aø­aØ­a
Case #4: <­a
Case #5: ˆ®ah®a


As you can see this doesn't make sene because the file should also contain the exact same things as the cmd outputs.
What's going on here ?
fprintf expects a c-string. You are using c++ string (surprised you didn't get warnings or even an error for this when compiling)

to fix:
fprintf(pFileO,"%s",words[i][words[i].size()-j-1].c_str());

The moral of this is that you should not be mixing c with c++, if you decide you wanted to do this in c++, you should be using fstream not FILE objects
Topic archived. No new replies allowed.