可能重复:
c ++ - 字符串上的 printf 打印乱码
我想将几个字符串写入文件。字符串是
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;
}