1题
#include < iostream>
int main()
{
using namespace std;
long num = 0;
char ch;
cout <<“请输入一些字符:\n”;
cin.get(ch);
cout <<" 字 符 前 边 的 字 符 是 : " ; w h i l e ( c h ! = ′ 字符前边的字符是: "; while (ch != ' 字符前边的字符是:";while(ch!=′$’)
{
cout << ch;
num++;
cin.get(ch);
}
cout <<"\n$前有" << num <<"个字符\n";
cout <<"输入流中还有这些字符: ";
while (ch != '\n')
{
cout << ch;
cin.get(ch);
}
cout <<".\n";
return 0;
}
///2题///
#include < iostream>
#include < fstream>
#include < cstdlib>
int main(int argc, char * argv[])
{
using namespace std;
char ch;
if (argc == 1)
{
cerr <<“Usage: " << argv[0] <<” filename[s]\n";
exit(EXIT_FAILURE);
}
ofstream fout;
fout.open(argv[1]);
if (!fout.is_open())
{
cerr <<"Could not open " << argv[1] << endl;
exit(9);
}
cout <<“请输入字符串到文件” << argv[1] <<“中.(ctrl + d 结束输入)\n”;
while (cin.get(ch))
fout << ch;
fout.close();
ifstream fin;
cout <<"查看文件" << argv[1] <<"中的内容:\n";
fin.open(argv[1]);
while (fin.get(ch))
cout << ch;
fin.close();
return 0;
}
//3题//
#include
#include
#include
int main(int argc, char * argv[])
{
using namespace std;
char ch;
ifstream fin;
ofstream fout;
if (argc > 3)
{
cerr << “命令行参数错误!\n”;
exit(1);
}
fin.open(argv[1]);
if (!