数组写入文件中 如何把数组的内容写入txt中啊?两个数组,数组长度100想把这2个数组分别保存在单一的TXT文档内怎么实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int i; FILE *fp; //保存a文件 if((fp=fopen("C:\\a.txt","w+"))!=NULL) { for(i=0; i<1000; i++) { fprintf(fp,"%d\n",a[i]); } fclose(fp); } else { MessageBox("写文件 a.txt 失败!", "错误"); //printf("写文件 a.txt 失败!\n"); //控制台程序使用这行 } //保存b文件 if((fp=fopen("C:\\b.txt","w+"))!=NULL) { for(i=0; i<100; i++) { fprintf(fp,"%d\n",b[i]); } fclose(fp); } else { MessageBox("写文件 b.txt 失败!", "错误"); //printf("写文件 b.txt 失败!\n"); //控制台程序使用这行 } #include <iostream>#include <fstream>#include <stdlib.h>using namespace std;const int N=100;char a[N]={'f','s','d','f','d','f','s','f','e','f'};void put_output_stream(ofstream& out_s);void get_output_stream(ofstream& out_s);int main(void){ ofstream out_s; get_output_stream(out_s); put_output_stream(out_s); out_s.close(); return 0;}void get_output_stream(ofstream& out_s){ char output_file[20]; cout << "Please input the input file name\n"; cin >> output_file; out_s.open(output_file); if (out_s.fail()) { cout << "output file opening failed\n"; exit(1); }}void put_output_stream(ofstream& out_s){ int i; for (i=0;i<N;i++) out_s << a[i];} 可以用 CFile 实现,也可以像 1楼那样。 没有为此解决方案配置选中要生成的项目//连debug都无法生成? WebBrowser 关闭问题 MFC中,为什么关闭单文档的框架窗口,程序就关闭了? 跪求VC++中socket传输gif图像 为什么有些机器浏览网页时无法显示VB Script调用的ActiveX控件? 为什么在dlg中添加RADIO在CLASSWIZARD的成员中看不到? 请问大虾们在vc里怎么调用win32api呢?最好有点示范源码。 如何培养面向对象的编程思想? 能做到吗? 高分在VC专辑找高手!!!!!问题解决了再加分!!!!! 高手帮忙 菜鸟排序问题
FILE *fp; //保存a文件
if((fp=fopen("C:\\a.txt","w+"))!=NULL)
{
for(i=0; i<1000; i++)
{
fprintf(fp,"%d\n",a[i]);
}
fclose(fp);
}
else
{
MessageBox("写文件 a.txt 失败!", "错误");
//printf("写文件 a.txt 失败!\n"); //控制台程序使用这行
} //保存b文件
if((fp=fopen("C:\\b.txt","w+"))!=NULL)
{
for(i=0; i<100; i++)
{
fprintf(fp,"%d\n",b[i]);
}
fclose(fp);
}
else
{
MessageBox("写文件 b.txt 失败!", "错误");
//printf("写文件 b.txt 失败!\n"); //控制台程序使用这行
}
#include <fstream>
#include <stdlib.h>using namespace std;const int N=100;char a[N]={'f','s','d','f','d','f','s','f','e','f'};void put_output_stream(ofstream& out_s);
void get_output_stream(ofstream& out_s);int main(void)
{
ofstream out_s; get_output_stream(out_s);
put_output_stream(out_s); out_s.close();
return 0;
}
void get_output_stream(ofstream& out_s)
{
char output_file[20];
cout << "Please input the input file name\n";
cin >> output_file; out_s.open(output_file);
if (out_s.fail())
{
cout << "output file opening failed\n";
exit(1);
}
}void put_output_stream(ofstream& out_s)
{
int i;
for (i=0;i<N;i++)
out_s << a[i];
}