程序名strFile
在linux环境下输入shell命令:
./strFlie filename1 [filename2]
将filename每行加上行号,输出到filename2中,若没有设置参数2,filename2=~filename1
strFile.cpp:
结果示例:
在linux环境下输入shell命令:
./strFlie filename1 [filename2]
将filename每行加上行号,输出到filename2中,若没有设置参数2,filename2=~filename1
strFile.cpp:
#include
<
sys
/
stat.h
>
#include < iostream >
#include < fstream >
#include < cstddef >
#include < cstdlib >
#include < string >
using namespace std;
class Cmyfile
{
private:
string fName;
size_t rowNum;
public:
ifstream infile;
Cmyfile(string str);
~Cmyfile();
string getFileName();
bool getLine(string& line);
} ;
Cmyfile::Cmyfile(string str):fName(str),rowNum( 0 ),infile(str.c_str())
{
if(!infile)
{
cerr<<"Cannot open "<<fName<<"!"<<endl;
exit(0);
}
}
Cmyfile:: ~ Cmyfile()
{}
string Cmyfile::getFileName()
{
return fName;
}
bool Cmyfile::getLine(string & line)
{
if(getline(infile,line))
return true;
return false;
}
int main( int argc, char * argv[])
{
if(argc==1)
{
cerr<<"Please input the file name!"<<endl;
exit(0);
}
Cmyfile thefile(argv[1]);
string outf("~");
if(argc>1)
outf=string(argv[2]);
else
outf+=argv[2];
ofstream outfile(outf.c_str());
string eline;
int icout=0;
while(thefile.getLine(eline))
{
char s[10],t[10];
sprintf(s,"/*%d*/",icout);
sprintf(t,"%-10s",s);
eline.insert(0,t);
outfile<<eline<<endl;
++icout;
}
cout<<"the primary file: "<<thefile.getFileName()<<endl;
cout<<" the dealed file: "<<outf<<endl;
}
#include < iostream >
#include < fstream >
#include < cstddef >
#include < cstdlib >
#include < string >
using namespace std;
class Cmyfile
{
private:
string fName;
size_t rowNum;
public:
ifstream infile;
Cmyfile(string str);
~Cmyfile();
string getFileName();
bool getLine(string& line);
} ;
Cmyfile::Cmyfile(string str):fName(str),rowNum( 0 ),infile(str.c_str())
{
if(!infile)
{
cerr<<"Cannot open "<<fName<<"!"<<endl;
exit(0);
}
}
Cmyfile:: ~ Cmyfile()
{}
string Cmyfile::getFileName()
{
return fName;
}
bool Cmyfile::getLine(string & line)
{
if(getline(infile,line))
return true;
return false;
}
int main( int argc, char * argv[])
{
if(argc==1)
{
cerr<<"Please input the file name!"<<endl;
exit(0);
}
Cmyfile thefile(argv[1]);
string outf("~");
if(argc>1)
outf=string(argv[2]);
else
outf+=argv[2];
ofstream outfile(outf.c_str());
string eline;
int icout=0;
while(thefile.getLine(eline))
{
char s[10],t[10];
sprintf(s,"/*%d*/",icout);
sprintf(t,"%-10s",s);
eline.insert(0,t);
outfile<<eline<<endl;
++icout;
}
cout<<"the primary file: "<<thefile.getFileName()<<endl;
cout<<" the dealed file: "<<outf<<endl;
}
/*0*/
#include
<
sys
/
stat.h
>
/*1*/ #include < iostream >
/*2*/ #include < fstream >
/*3*/ #include < cstddef >
/*4*/ #include < cstdlib >
/*5*/ #include < string >
/*6*/
/*7*/ using namespace std;
/*8*/
/*9*/ class Cmyfile
.....
/*1*/ #include < iostream >
/*2*/ #include < fstream >
/*3*/ #include < cstddef >
/*4*/ #include < cstdlib >
/*5*/ #include < string >
/*6*/
/*7*/ using namespace std;
/*8*/
/*9*/ class Cmyfile
.....