作者holder319 (大头)
看板NCTU-STAT94G
标题Re: [心得] fprintf 改档名自动输出
时间Fri Oct 27 11:51:07 2006
※ 引述《catherinejoy (whatcanisay)》之铭言:
若你把副档名改成.cpp,然後在开头 include "iostream" "fstream" "cstring",
那下面的code会更简洁,输出输入也不再需要手动设定格式,一切有预设值自动化.
(当然,做文字处理时你也可以include "string" 然後跟 "cstring" 交叉转换使用)
ex:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string str; //也可事先在此指定 string str = "../123/abc.txt" (相对路径)
cout<<"input path and filename \n";
cin>>str; // 输入档名与要输出路径,路径预设值是目前资料夹
// 因为每个人的电脑系统不一样,故用相对路径会比绝对路径好
ofstream ofile(str.c_str()); // 建立要输出的档案
ofile<<"\n a.xxx = "<<123 // 输出东西到档案
<<"\n b.yyy = "<<456.789
<<"\n ";
ofile.close();
}
: main 里
: //export_txt('D:/2006_fall/NHRI','delA',ans,del,item);
: printf("Please input the FILE NAME:\n");
: scanf("%s",&filename);//输入档名开头
: for(i=0;i<del;i++){
: sprintf(filename,"%s_%d",filename,i);//filename_i.txt
: //path="D:/2006_fall/NHRI/i";
: sprintf(path,"%s/%d","D:/2006_fall/NHRI",i);//也可以改路径
: export_txt(path,filename,ans,del,item);
: printf("\n%s.txt is OK!!!\n",filename);
: }//end for i
: header
: void export_txt(char *path,char *FileName,double **ans,int nrow, int ncol){
: char Name[1024];
: int i,j;
: sprintf(Name,"%s/%s.txt",path,FileName);
: out = fopen(Name,"w");
: for(i=0;i<nrow;i++){
: fprintf(out,"%d\t",i+1);
: for(j=0;j<ncol;j++){
: fprintf(out,"%3.0lf\t",ans[i][j]);
: }//end for j
: fprintf(out,"\n");
: }//end for i
: }
: 好 请享用...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.212.111
※ 编辑: holder319 来自: 140.113.212.111 (10/27 11:52)
※ 编辑: holder319 来自: 140.113.114.85 (10/28 02:27)
※ 编辑: holder319 来自: 140.113.114.85 (10/28 02:30)
1F:推 catherinejoy:大力推.... 10/29 21:00