我已经有了一个string word[n]数组,每个单元保存了一个单词。
现在需要把它们输出到一个txt文件中,要求写一个换一行。
但怎么整都不行,试了fprintf()和fwrite()都失败了,输出在文件中的要么都是字符串“NULL”要么就是空的。。。坐等高人指教~
--------------------------------------代码在这,写出来的是乱码和空白,哎
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string word[4];
word[0] = "What";
word[1] = "a";
word[2] = "f*cking";
word[3] = "day";
FILE *pfOutput;
pfOutput = fopen("output.txt", "wt");
if (pfOutput == NULL)
cout << "Error!" << endl;
else
cout << "output.txt open success~" << endl;
for(int i=0; i<4; i++)
{
fwrite(&word[i], word[i].length(), 1, pfOutput);
fprintf(pfOutput, "\n" );
}
return 0;
}
当然可以了,任何C或C++的办法都欢迎啊!