1题////////////////////////////////////////////////////////////////////////////////////////////
#include < iostream>
int main()
{
using namespace std;
long num = 0;
char ch;
cout <<“请输入一些字符:\n”;
cin.get(ch);
cout <<"字符前边的字符是:";while(ch!=′字符前边的字符是: "; 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 (!fin.is_open())
{
cerr << argv[1] <<“文件无法打开!\n”;
exit(2);
}
fout.open(argv[2]);
if (!fout.is_open())
{
cerr << argv[2] <<“文件无法打开!\n”;
exit(3);
}
while (fin.get(ch))
fout << ch;
fin.close();
fout.close();
cout &

本文提供了一系列C++编程练习题,涉及字符输入输出、文件读写及命令行参数处理。题目包括从标准输入读取字符并输出、将字符写入文件、从多个文件读取并将内容合并到一个文件、读取文件并按行存储以及使用自定义类处理文件存储等操作。
最低0.47元/天 解锁文章
418

被折叠的 条评论
为什么被折叠?



