文本转换linux和windows环境下的不同

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文件


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值