C++ cross-compile not handling newline input text file -
for clarity:
this not duplicate of getting std :: ifstream handle lf, cr, , crlf?
this extension of c++ cutting off character(s) when read lines file
i state front because when posted question @ c++ cutting off character(s) when read lines file tagged potential duplicate of getting std :: ifstream handle lf, cr, , crlf?. tried simplified version (direct read instead of buffers keep simple) of proposed solution @ other post did not work me , though edited question , code demonstrate that, there has been no responses. jonathon suggested re-post separate question here am.
i have tried numerous other solutions, ending code below although code handles tabs , normal text expected, still not handling newline character differences expected need help.
i want to:
- read in contents of txt file
- run validation checks on content
- output report txt file
in prototype code reading in text 1 file , outputting edited text file. after working, i'll worry running validation tests, ...
i compiling , testing on linux mint maya (ubuntu 12.04 based) box , cross-compiling mingw32 run on windows pc.
everything works fine when i:
- compile , run on linux box linux-created text file
- cross-compile on linux , run on windows linux-created text file
however, when i:
- cross-compile on linux , run on windows windows-created text file
the result not expected; first few characters skipped.
i need program handle either windows-created or linux-created text files.
the (silly content test) input file using in cases (one created on linux box; 1 created on windows using notepad) :
a new beginning in case file corrupted , darn program working fine ... @ least on linux
when read file in , use program (code shown below) linux-created text file produces proper output:
line 1: new beginning line 2: in case line 3: file corrupted line 4: , darn program working fine ... line 5: @ least on linux
when use windows-created text file , run program on windows pc, output is:
line 1: new beginning line 2: t in case line 3: e file corrupted line 4: nd darn program working fine ... line 5: @ least on linux
as can see, there characters missing lines 2,3,4 not 1,5:
- 0 characters missing start of line 1
- 3 characters missing start of line 2
- 2 characters missing start of line 3
- 1 characters missing start of line 4
- 0 characters missing start of line 5
i expect has differences in handling of newline in linux , windows text files have read other postings on , tried solutions not seem solving issue. sure missing basic , apologize in advance if so, i've been banging away @ on week , need help.
the code using is:
int main(int argc, char** argv) { /* *program to: * 1) read text file * 2) validation checks on content of text file * 3) output report text file */ std::string rc_input_file_name = "rc_input_file.txt"; std::string rc_output_file_name = "rc_output_file.txt"; char *rc_input_file_name = new char[ rc_input_file_name.length() + 1 ]; strcpy( rc_input_file_name, rc_input_file_name.c_str() ); char *rc_output_file_name = new char[ rc_output_file_name.length() + 1 ]; strcpy( rc_output_file_name, rc_output_file_name.c_str() ); std::ifstream rc_input_file_holder; rc_input_file_holder.open( rc_input_file_name , std::ios::in ); if ( ! rc_input_file_holder.is_open() ) { std::cout << "error - not open input file" << std::endl; return exit_failure; } else { std::ofstream rc_output_file_holder; rc_output_file_holder.open( rc_output_file_name , std::ios::out | std::ios::trunc ); if ( ! rc_output_file_holder.is_open() ) { std::cout << "error - not open or create output file" << std::endl; return exit_failure; } else { std::streampos char_num = 0; long int line_num = 0; long int starting_char_pos = 0; std::string file_line = ""; while ( getline( rc_input_file_holder , file_line ) ) { line_num = line_num + 1; long unsigned file_line_length = file_line.length(); std::string string_to_find = "\r"; std::string string_to_insert = "\n"; long unsigned num_char_in_string_to_find = string_to_find.length(); long unsigned character_position; while ( ( character_position = file_line.find( string_to_find ) ) != std::string::npos ) { if ( character_position == file_line_length - num_char_in_string_to_find ) { // if \r character found @ end of line, // old mac style newline, // replace \n file_line.replace( character_position , num_char_in_string_to_find , string_to_insert ); file_line_length = file_line.length(); } else { // if \r character found not last character in line // second-last character meaning windows newline pair \r\n // or somewhere in middle of line // delete file_line.erase( character_position , num_char_in_string_to_find ); file_line_length = file_line.length(); } } int field_display_width = 4; rc_output_file_holder << "line " << line_num << ": " << file_line << std::endl; starting_char_pos = rc_input_file_holder.tellg(); } rc_input_file_holder.close(); rc_output_file_holder.close(); delete [] rc_input_file_name; rc_input_file_name = 0; delete [] rc_output_file_name; rc_output_file_name = 0; } } }
any , suggestions appreciated ...
well, thank martin schlott tried program on compiler , worked text files either windows or linux sources.
this pointed me compiler differences , key.
the cross-compiler installed apt-get install mingw32 put older compiler (v4.2.1) cross-compile apt-get install g++ put linux compiler in @ v 4.6.2.
so found old listing on sourceforge v4.6.3 of cross-compiler mingw g++ v4.6.3
, installed it.
i had include path of new install , had add 2 options in compile command
- -static-libgcc
- -static-libstdc++
to prevent 2 "missing dll" error messages.
after that, cross-compile worked cleanly , newline differences handled no problem.
i love technology: spending on week thinking doing wrong , cross-compiler out of date. oh well, learned lot c++ in meantime , helps else in future.
thanks again
r
Comments
Post a Comment