1.
#include<iostream>
using namespace std;
int main()
{
cout<<"enter the string:";
int count=0;
char ch;
while(cin.peek()!='$'&&cin.get(ch))//先将peek函数放在前面,在进行读取字符
count++;
cout<<count<<endl;
return 0;
}
2.
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char fname[50];
char inf[100];
ofstream fout;
cout<<"enter the name of file:";
cin>>fname;
fout.open(fname);
cout<<"enter the information:";
cin.get(); //cin将enter留在了输入流中,因此需要将enter去除
cin.get(inf,100);
fout<<inf;
fout.close();
return 0;
}
4.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream fin1("zhu.txt");
ifstream fin2("yue.txt");
ofstream fout("zhuyue.txt");
char ch1[100],ch2[100];
while(fin1.peek()!='\n'&&fin1.getline(ch1,100,'\n'))
{
fout<<ch1;
fout<<" ";
if(fin2.peek()!='\n'&&fin2.getline(ch2,100,'\n'))
fout<<ch2;
fout<<endl;
}
while(fin2.peek()!='\n'&&fin2.getline(ch2,100,'\n'))
fout<<ch2<<endl;
return 0;
}
5.
#include<iostream>
#include<fstream>
#include<list>
#include<string>
using namespace std;
int main()
{
list<string> mat;
list<string> pat;
char str[20];
ifstream fin;
ofstream fout("matpat.txt");
fin.open("mat.txt");
while(fin.getline(str,20,'\n'))
mat.push_back(str);
mat.sort();
for(list<string>::iterator it=mat.begin();it!=mat.end();it++)
cout<<(*it)<<endl;
fin.close();
fin.clear();//close只是将文件与流之间的关系切断,还需要clear,否则再次使用fin时其文件指针指向不对
fin.open("pat.txt");
while(fin.getline(str,20,'\n'))
pat.push_back(str);
pat.sort();
for(it=pat.begin();it!=pat.end();it++)
cout<<(*it)<<endl;
fin.close();
fin.clear();
mat.merge(pat);
mat.unique();
for(it=mat.begin();it!=mat.end();it++)
fout<<*it<<endl;
return 0;
}