📄 仿dos.cpp
字号:
#include<string>
#include<iomanip>
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
string str1,str2,str3;
char ch;
if (argc == 1)
{
cout << "Please input the operation: ";
getline(cin, str1,' ');
}
else
str1 = argv[1];
if(str1=="mtype")
{
getline(cin, str2);
//fstream constructor opens file
ifstream fcin(str2.c_str());
if(fcin.fail())
{
//file not found
cout<<"the file could not be opened"<<endl;
return 1;
}
else //filter poiter at the first position
fcin.seekg(0);
while(!fcin.eof())
{
fcin.get(ch);
cout<<ch;
}
cout<<endl;
}else
if(str1=="mcopy")
{
getline(cin, str2,' ');
getline(cin, str3);
ifstream fcin(str2.c_str());
ofstream fcout(str3.c_str());
if(fcin.fail())
{
//file not found
cout<<"the file could not be opened"<<endl;
return 1;
}
else
if(fcout.fail())
{
//file not saved
cout<<"the file could not be saved"<<endl;
return 1;
}
else //filter poiter at the first position
fcin.seekg(0);
while(!fcin.eof())
{
fcin.get(ch);
if(!fcin.eof())
fcout.put(ch);
}
}
else
if(str1=="mcompare")
{
string compare1="",compare2="";
getline(cin, str2,' ');
getline(cin, str3);
ifstream fcin(str2.c_str());
if(fcin.fail())
{
//file not found
cout<<"the file1 could not be opened"<<endl;
return 1;
}
fcin.seekg(0);
while(!fcin.eof())
{
fcin.get(ch);
compare1+=ch;
}
ifstream fcin2(str3.c_str());
if(fcin2.fail())
{
//file not found
cout<<"the file2 could not be opened"<<endl;
return 1;
}
fcin2.seekg(0);
while(!fcin2.eof())
{
fcin2.get(ch);
compare2+=ch;
}
int f=compare1.compare(compare2);
if(f==0)
cout<<"they are the same in the content"<<endl;
else
cout<<"they are not the same in the content"<<endl;
if(compare1.length()==compare2.length())
cout<<"file1 is the same lengh with file2"<<endl;
else if(compare1.length()>compare2.length())
cout<<"file1 is longer than file2"<<endl;
else
cout<<"file2 is longer than file1"<<endl;
}
else
{
cout<<"can not recognise the operation!"<<endl;
cout<<"please input the operation--mtype--mcopy--mcompare."<<endl;
cout<<"for example:"<<endl;
cout<<setw(10)<<"mtype 123.txt\n"
<<setw(10)<<"mcopy 123.txt 234.txt\n"
<<setw(10)<<"mtype 123.txt 234.txt\n";
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -