我在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