c++ - Program output differs between Windows and Linux. Why? -
i run code in both windows , linux. in window, can result intended but, in linux, different result 1 window.
what causes difference , how fix code in linux?
thanks lot! :) attached code, input, , result both os.
below code; (this code reversely order components dots , differentiate components using slash.)
#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 = 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 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(); (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; }
this inputs in 'raw_file' declared in code;
/blog.sina.com.cn/mouzhongshao /blogs.yahoo.co.jp/junkii3/11821140.html /allplayboys.imgur.com
this result window; (this want above code)
/cn/com/sina/blog/mouzhongshao/ /jp/co/yahoo/blogs/junkii3/html/11821140/ /com/imgur/allplayboys/
this result linux; (unexpected result)
/cn/com/sina/blog/mouzhongshao / /jp/co/yahoo/blogs/junkii3/html /11821140/ /com /imgur/allplayboys/
windows uses compound end of line: carriage return , line feed (\r\n
). when c++ file stream opened file in text mode, default, finds \r\n
, silently converts \n
.
linux uses line feed (\n
). when file stream finds \r\n
, \r
treated regular character , passed parser.
so on linux /blog.sina.com.cn/mouzhongshao\r\n
broken
<empty> blog sina com cn mouzhongshao\r
and depending on how console handles \r
may print
/cn/com/sina/blog/mouzhongshao /
or
/cn/com/sina/blog/mouzhongshao
with carriage return moving cursor beginning of line , overwriting first /
last.
the easy solution convert input file linux-style line endings. many linux text editors have dos unix format conversion utility built in. dos2unix application available. if else fails, rewrite file under linux.
a longer solution make both windows , linux behave same. many examples of exist. here one: getting std :: ifstream handle lf, cr, , crlf?
also watch out while (!infile.fail())
tests readability before reading, meaning subsequent reads may fail , won't know. more on here: why iostream::eof inside loop condition considered wrong?
to resolve this, not cast result of infile.get();
char
keep int
long enough see if result traits::eof()
before using value char
.
Comments
Post a Comment