可能重複:
c++ - printf on strings prints gibberishfprintf中,字符串和向量
我想寫一些字符串文件。字符串是
37 1 0 0 0 0
15 1 0 0 0 0
33 1 0 0 0 0
29 1 0 0 0 0
18 1 0 0 0 0
25 1 0 0 0 0
我第一想用來存儲每行作爲一個字符串數組的元素,然後調用相同的字符串陣列和寫其到文件元件。
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
int writeFile() {
char line[100];
char* fname_r = "someFile_r.txt"
char* fname_w = "someFile_w.txt";
vector<string> vec;
FILE fp_r = fopen(fname_r, "r");
if(fgets(line, 256,fp_r) != NULL) {
vec.push_back(line);
}
FILE fp_w = fopen(fname_w, "w");
for(int j = 0; j< vec.size(); j++) {
fprintf(fp_w, "%s", vec[j]); // What did I miss? I get funny symbols here. I am expecting an ASCII
}
fclose(fp_w);
fclose(fp_r);
return 0;
}
http://stackoverflow.com/questions/3634766/c-printf-on-strings-prints-gibberish –
你正在用C++編寫C代碼。停下來。 – rubenvb