cerr 一般用来输出错误信息
然后打开一个文件 ,写入内容,关闭(保存) ,再打开输出
//命令行处理技术 p772
//以下命令打开程序未命名3
#include<iostream>
#include<cstdlib>
#include<fstream>
int main(int argc,char *argv[])
{
using namespace std;
if(argc == 1)
{
cerr << "Usage: " << argv[0] << " filename[s]\n";//一般cerr 输出错误信息警告使用者
exit(EXIT_FAILURE);
}
ifstream fin;
long count;
long total = 0;
char ch;
for(int file = 1;file < argc; file++)
{
fin.open(argv[file]); //connect stream to argv[file]
if(!fin.is_open())
{
cerr << "Could not open " << argv[file] << endl;
fin.clear();
continue;
}
count = 0;
while (fin.get(ch))
count++;
cout << count << " characters in " << argv[file] << endl;
total+= count;
fin.clear();
fin.close();
}
cout << total << " characters in all files\n";
return 0;
}
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string filename;
cout << "Enter name for new file: ";
cin >> filename;
//打开记事本输入内容
ofstream fout(filename.c_str());
fout << "FOR your eyes only!\n";
cout << "Enter your secret number: ";
float secret;
cin >> secret;
fout << "your secret number is " << secret <<endl;
fout.close();
//关闭记事本,并保存内容
//再次打开记事本,读取记事本内容
ifstream fin(filename.c_str());
cout << "Here are the contents of " << filename << ":\n";
char ch;
while(fin.get(ch))
cout << ch;
cout << "Done\n";
fin.close();
return 0;
}
学会 怎么用switch 和 case
搞了好久
#include<iostream>
using namespace std;
int main()
{
int k;
char c;
for(k=1,c='A'; c < 'F'; k++)
{
switch(++c)
{
case'A': k++; printf("%c %d\n",c,k);
cout << "a" << endl;
break;
case'B': k *= 2; printf("%c %d\n",c,k);
cout << "b" << endl;
break;
//跳出switch()执行其后的语句
case'C': k--; printf("%c %d\n",c,k);
cout << "c" << endl; //不论条件为何值,继续执行下一条case判断(case'D':)后面的语句
case'D': k %= 3; printf("%c %d\n",c,k);
cout << "d" << endl;
continue; //不执行switch块后面的语句,跳出“本次”循环直接到外层循环
//所有条件不符合,执行default后面的语句
}
k++;
cout << "*********************\n";
}
cout << k << endl;
return 0;
}
最最最重要的好
#include<iostream>
#include<cmath>
#include<cstring>
#include<iomanip> //
using namespace std;
int main()
{
cout << fixed << right; 右对齐 一定不能掉了
cout << setw(6) << "N" << setw(14) << "square root" << setw(15) << "fourth root \n";
double root;
for(int n = 10; n <= 100; n += 10)
{
root = sqrt(double(n));
cout << setw(6) << setfill('.') << n << setfill (' ') setfill 是用啥填满 setw 是有多少格子 setprecision 是设置输出精度
<< setw(12) << setprecision(3) << root << setw(14) << setprecision(4) <<sqrt(root) << endl;
}
return 0;
}