我在Windows和Linux中运行代码。 在Window中,我可以得到我想要的结果,但在Linux中,我从Window获得了不同的结果。Windows和Linux之间的程序输出不同。为什么?
是什么导致了这种差异,以及如何修复Linux中的代码?
非常感谢! :)我附加了代码,输入和来自两个操作系统的结果。
以下是我的代码; (该代码是反向订购带有圆点的组件和使用斜线区分组件。)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
string set_name = "6000k";
// in
string raw_file = set_name + "_filtered.txt";
// out
string set_file = set_name + "_filtered_dot.txt";
// main
int main()
{
int i = 0;
string comp = "";
string str;
vector<string> input_comp;
vector<string> tmp_comp;
int input_order = 0;
ifstream infile;
infile.open(raw_file);
ofstream outfile;
outfile.open(set_file);
if (infile.fail()) // error handling
{
cout << "error; raw_file cannot be open..\n";
}
while (!infile.fail())
{
char c = infile.get();
if (c == '\n' || c == '/')
{
if (comp != "")
{
input_comp.push_back(comp);
}
int num = input_comp.size();
for (int j = 0; j < num; j++)
{
int idx = (num - 1) - j;
outfile << "/" << input_comp[idx];
}
if (c == '\n')
{
outfile << "/" << endl;
}
input_comp.clear();
str = "";
comp = "";
}
else if (c == '.')
{
if (comp != "")
{
input_comp.push_back(comp);
}
comp = "";
}
else
{
str = c;
comp = comp + str;
}
}
infile.close();
outfile.close();
return 0;
}
这在代码声明为“raw_file”输入;
/blog.sina.com.cn/mouzhongshao
/blogs.yahoo.co.jp/junkii3/11821140.html
/allplayboys.imgur.com
这是Window的结果; (这是我想从代码上面得到的)
/cn/com/sina/blog/mouzhongshao/
/jp/co/yahoo/blogs/junkii3/html/11821140/
/com/imgur/allplayboys/
这是Linux的结果; (意外结果)
'while(!infile.fail())'在读取之前检查失败。不要指望这个工作。 – user4581301
最终值似乎包括linux上的换行符(例如''html \ n“'而不是''html'') – Justin
如果输入文件是在Windows上创建的,它将包含窗口行尾:\ r \ ñ。这会在Linux下搞乱你的输出,因为它会打印\ r。 – user4581301