LINUX
#include<iostream> #include<fstream> #include<sstream> using namespace std; double strConvert(string str) { stringstream stream; double number=0; stream<<str; stream>>number; return number; } string douConvert(double dou) { stringstream stream; stream<<dou; return stream.str(); } int main(int argc,char* args[]) { ifstream inFile(args[1]);//the source document ofstream outFile(args[2]);//the goal if(!inFile) { cout<<"Open InFile Failure!"; return 0 ; } if(!outFile) { cout<<"Open OutFile Failure!"; return 0; } string text; while(getline(inFile,text))//get the line once { if(text[text.length()-1]=='\r') { text=text.substr(0,text.length()-1); } char ch=text[text.length()-1]; if(ch=='m') { text=text.substr(0,text.length()-1); } if(ch=='g') { text=text.substr(0,text.length()-1); text=douConvert(strConvert(text)*1204); } outFile<<text<<endl; } cout<<"OK!"; return 0; }
windows
#include<iostream> #include<fstream> #include<sstream> using namespace std; double strConvert(string str) { stringstream stream; double number=0; stream<<str; stream>>number; return number; } string douConvert(double dou) { stringstream stream; stream<<dou; return stream.str(); } int main(int argc,char* args[]) { ifstream inFile(args[1]);//the source document ofstream outFile(args[2]);//the goal if(!inFile) { cout<<"Open InFile Failure!"; return 0; } if(!outFile) { cout<<"Open OutFile Failure!"; return 0; } string text; while(getline(inFile,text))//get the line once { char ch=text[text.length()-1]; if(ch=='m') { text=text.substr(0,text.length()-1); } if(ch=='g') { text=text.substr(0,text.length()-1); text=douConvert(strConvert(text)*1024); //<<strConvert(text)*1024<<endl; } outFile<<text<<endl; } cout<<"OK!"; return 0; }
总结:1、换行符的问题,说是windows中换行符使用\r\n表示,而在linux中换行符使用\n表示,所以getline按行读取文本时去掉了\n,但是\r还是保留的,所以移植到linux中需要对\r进行处理
2、在windows下使用gcc编译器,将gcc安装目录前的bin路径添加到环境变量的path中,在命令行可以编译执行,注意main函数中的(int argc,char* args[])的使用,args[0]表示编译后的文件名称,args[1]表示第一个程序中使用到的参数
3.linux编译,使用的是SecureCRT,编译命令 g++ -Wall -o test test.cpp,其中test是编译后的目标文件名称,test.cpp是需要进行编译的源cpp文件