test_io.cpp
来自「C++ Math Lib. C++ Builder must use.」· C++ 代码 · 共 189 行
CPP
189 行
//---------------------------------------------------------------------------
// N.V.Shokhirev
// created: 20041020
// modified: 20051020
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include "uIO.h"
//---------------------------------------------------------------------------
#pragma argsused
using namespace std;
enum variable {var_, vars, varn, vara, varb, varc, varv, varvv};
int main(int argc, char* argv[])
{
char s1[64], s2[64], ss[64];
string s;
FArr1D v(2,9);
FArr2D vv(2,3);
int n = 12;
double a = 1.2345678901234567890;
double b = 1.2345678901234567890e-33;
double c = 0.0000000012345678901234567890;
// ofstream outfile ("test.txt");
ofstream outfile;
ifstream infile;
map<string,variable> varmap;
varmap["n"] = varn;
varmap["a"] = vara;
varmap["b"] = varb;
varmap["c"] = varc;
varmap["v"] = varv;
varmap["vv"] = varvv;
varmap["s"] = vars;
varmap["_"] = var_;
// init all
for(int i=v.L1(); i<=v.H1(); i++) v(i) = a*i;
v(5) = 0.0000000000123456789;
for(int i1=vv.L1(); i1<=vv.H1(); i1++)
for(int i2=vv.L2(); i2<=vv.H2(); i2++) vv(i1,i2) = a*(i1+i2);
outfile.open("test.txt");
outfile.precision(9);
writevalue(outfile, "n", n);
writevalue(outfile, "s", "string 1");
writevalue(outfile, "c", c);
writeFArr1D(outfile, "v", v);
writevalue(outfile, "b", b);
writeFArr2D(outfile, "vv", vv);
writevalue(outfile, "a", a);
outfile.close();
// erase all
n = 0; a = 0.0; b = 0.0; c = 0.0;
for(int i=v.L1(); i<=v.H1(); i++) v(i) = 0.0;
for(int i1=vv.L1(); i1<=vv.H1(); i1++)
for(int i2=vv.L2(); i2<=vv.H2(); i2++)
vv(i1,i2) = 0.0;
infile.open("test.txt");
while (infile.good())
{
infile.getline(s1, 64,'=');
infile.getline(s2, 64);
s = s1;
if (s == "") s ="_";;
switch (varmap[s])
{
case varn: sscanf(s2,"%d", &n); break;
case vara: sscanf(s2,"%lf", &a); break;
case varb: sscanf(s2,"%lf", &b); break;
case varc: sscanf(s2,"%lf", &c); break;
case vars: strcpy(ss, s2); break;
case varv: readFArr1D(infile, v); break;
case varvv: readFArr2D(infile, vv); break;
default:;
};
};
infile.close();
// check
cout << "n=" << n << endl;
cout << "s=" << ss << endl;
cout << "c=" << c << endl;
for(int i=v.L1(); i<=v.H1(); i++)
cout << "v[" << i << "]=" << v(i) << endl;
cout << "b=" << b << endl;
for(int i1=vv.L1(); i1<=vv.H1(); i1++)
{
for(int i2=vv.L2(); i2<=vv.H2(); i2++)
cout << "vv[" << i1 << "," << i2 <<"]=" << vv(i1,i2) << endl;
};
cout << "a=" << a << endl;
cout << "Press any key ... ";
getchar();
// TIME ===============================================
/*
struct tm {
int tm_sec; // Seconds
int tm_min; // Minutes
int tm_hour; // Hour (0--23)
int tm_mday; // Day of month (1--31)
int tm_mon; // Month (0--11)
int tm_year; // Year (calendar year minus 1900)
int tm_wday; // Weekday (0--6; Sunday = 0)
int tm_yday; // Day of year (0--365)
int tm_isdst; // 0 if daylight savings time is not in effect)
};
*/
struct tm t;
char str[80];
//* sample loading of tm structure
t.tm_sec = 1; //* Seconds
t.tm_min = 30; //* Minutes
t.tm_hour = 9; //* Hour
t.tm_mday = 22; //* Day of the Month
t.tm_mon = 11; //* Month
t.tm_year = 56; //* Year - does not include century
t.tm_wday = 4; //* Day of the week
t.tm_yday = 0; //* Does not show in asctime
t.tm_isdst = 0; //* Is Daylight SavTime; does not show in asctime
//* converts structure to null terminated string
strcpy(str, asctime(&t));
printf("%s\n", str);
// getchar();
// typedef long time_t;
time_t timer;
struct tm *tblock;
//* gets time of day
timer = time(NULL);
//* converts date/time to a structure
tblock = localtime(&timer);
//Prototype: char *asctime(const struct tm *tblock);
printf("sec = %d\n", tblock->tm_sec );
printf("min = %d\n", tblock->tm_min );
printf("hour = %d\n", tblock->tm_hour );
printf("mday = %d\n", tblock->tm_mday );
printf("mon = %d\n", tblock->tm_mon );
printf("year = %d\n", tblock->tm_year );
printf("wday = %d\n", tblock->tm_wday );
printf("yday = %d\n", tblock->tm_yday );
printf("isdst= %d\n", tblock->tm_isdst);
printf("Local time is: %s\n", asctime(tblock));
time(&timer);
// Prototype: char *ctime(const time_t *time);
printf("Today's date and time: %s\n", ctime(&timer));
cout << "Press any key ... ";
getchar();
ofstream out("test2.txt");
writeDatetime(out, tblock );
out.close();
ifstream in("test2.txt");
readDatetime(in, tblock);
in.close();
printf("Local time is: %s\n", asctime(tblock));
cout << "Press any key ... ";
getchar();
return 0;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?