C ++交叉编译不处理换行输入文本文件 [英] C++ cross-compile not handling newline input text file

查看:83
本文介绍了C ++交叉编译不处理换行输入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为清楚起见:

这不是获取std :: ifstream的副本来处理LF,CR和CRLF?

这是 C ++截断字符的扩展),当从文件中读取行时

我事先声明是因为当我在 C ++从文件中读取行时会截断字符,它被标记为

I state this up front because when I posted the question at C++ cutting off character(s) when read lines from file it was tagged as a potential duplicate of Getting std :: ifstream to handle LF, CR, and CRLF?. I tried a simplified version (direct read instead of buffers to keep it simple) of the proposed solution at the other post it did not work for me and even though I edited my question and the code to demonstrate that, there has been no responses. Jonathon suggested I re-post as a separate question so here I am.

我还尝试了许多其他解决方案,最终得到下面的代码,但是尽管该代码按预期方式处理了制表符和普通文本,但仍无法按预期方式处理换行符,因此我需要帮助.

I have also tried numerous other solutions, ending up with the code below but although the code handles tabs and normal text as expected, it is still not handling the newline character differences as was expected so I need help.

我要:

  • 读取txt文件的内容
  • 对内容进行一些验证检查
  • 将报告输出到另一个txt文件

在此原型代码中,我只是从一个文件中读取文本,然后将编辑后的文本输出到另一个文件中.完成这项工作后,我将担心运行验证测试,...

In this prototype code I am just reading in text from one file and outputting edited text to another file. After I get this working, I'll then worry about running validation tests, ...

我正在Linux Mint Maya(基于Ubuntu 12.04)盒上进行编译和测试,然后与mingw32交叉编译以在Windows PC上运行.

I am compiling and testing on a Linux Mint Maya (Ubuntu 12.04 based) box and then cross-compiling with mingw32 to run on a Windows PC.

当我:一切正常时

  • 使用Linux创建的文本文件在Linux盒上编译并运行
  • 在Linux上交叉编译并在Windows上使用linux创建的文本文件运行

但是,当我:

  • 在Linux上交叉编译并使用Windows创建的文本文件在Windows上运行

结果与预期不符;前几个字符被跳过.

the result is not as expected; the first few characters are skipped.

我需要该程序来处理Windows创建或Linux创建的文本文件.

I need the program to handle either Windows-created or linux-created text files.

在所有情况下(一个在Linux盒子上创建;一个在Windows上使用记事本创建的)我正在使用的(现在只是测试的愚蠢内容)输入文件是:

The (silly content for now just as a test) input file I am using in all cases (one created on linux box; one created on Windows using Notepad) is :

A new beginning
just in case
the file was corrupted
and the darn program was working fine ...
at least it was on linux

当我读入文件并使用程序(如下所示的代码)时,由linux创建的文本文件会产生正确的输出:

When I read the file in and use the program (code shown below) the linux-created text file produces the proper output:

Line 1: A new beginning
Line 2: just in case
Line 3: the file was corrupted
Line 4: and the darn program was working fine ...
Line 5: at least it was on linux

当我使用Windows创建的文本文件并在Windows PC上运行该程序时,输出为:

When I use the Windows-created text file and run the program on a Windows PC, the output is:

Line 1: A new beginning
Line 2: t in case
Line 3: e file was corrupted
Line 4: nd the darn program was working fine ...
Line 5: at least it was on linux

如您所见,第2,3,4行缺少字符,但1,5行中没有:

As you can see, there are characters missing from lines 2,3,4 but not from 1,5:

    第1行开头缺少
  • 0个字符
  • 从第2行开始缺少3个字符
  • 在第3行的开头缺少
  • 2个字符
  • 在第4行的开头缺少
  • 1个字符
  • 在第5行的开头缺少
  • 0个字符
  • 0 characters missing from the start of line 1
  • 3 characters missing from the start of line 2
  • 2 characters missing from the start of line 3
  • 1 characters missing from the start of line 4
  • 0 characters missing from the start of line 5

我希望这与处理linux和Windows文本文件中的换行符有所不同,但是我已经阅读了有关此内容的其他文章并尝试了解决方案,但似乎并不能解决问题.我确定我会遗漏一些非常基本的内容,如果这样的话,请提前道歉,但是我已经为此努力了一个多星期,并且需要帮助.

I expect this has something to do with the differences in handling of newline in linux and Windows text files but I have read the other postings on this and tried the solutions but it does not seem to be solving the issue. I am sure I am missing something very basic and apologize in advance if so, but I've been banging away at this for over a week and need help.

我正在使用的代码是:

int main(int argc, char** argv)
{


    /*
     *Program to:
     *  1) read from a text file
     *  2) do some validation checks on the content of that text file
     *  3) output a report to another 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 - Could not open the 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 - Could not open or create the 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 the \r character is found at the end of the line, 
                        //   it is the old Mac style newline, 
                        //   so replace it with \n
                        file_line.replace( character_position , num_char_in_string_to_find , string_to_insert );
                        file_line_length = file_line.length();
                    }
                    else
                    {
                        // If the \r character is found but is not the last character in the line
                        //   it could be the second-last character meaning it is a Windows newline pair \r\n
                        //   or it could be somewhere in the middle of the line
                        //   so delete it
                        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 and all suggestions appreciated ...

推荐答案

非常感谢Martin Schlott,他在自己的编译器上尝试了我的程序,该程序可以处理Windows或Linux来源的文本文件.

Well, Thank you to Martin Schlott who tried my program on his compiler and it worked with text files from either Windows or Linux sources.

这使我注意到了编译器之间的差异,这是关键.

This pointed me to the compiler differences and that was the key.

apt-get install mingw32安装的交叉编译器为交叉编译添加了较旧的编译器(v4.2.1),而apt-get install g ++将Linux编译器置于4.6.2版.

The cross-compiler installed by apt-get install mingw32 put an older compiler (v4.2.1) for the cross-compile but the apt-get install g++ put the linux compiler in at v 4.6.2.

因此,我在sourceforge上找到了一个旧版本的交叉编译器v4.6.3版本 Mingw与G ++ v4 .6.3
并安装它.

So I found an old listing on sourceforge for a v4.6.3 of the cross-compiler Mingw with G++ v4.6.3
and installed it.

我必须包括新安装的路径,并且必须在编译命令中添加两个选项

I had to include the path of the new install and I had to add two options in the compile command

  • -static-libgcc
  • -static-libstdc ++

防止出现2条缺少dll"错误消息.

to prevent 2 "missing dll" error messages.

此后,交叉编译工作顺利,换行符之间的差异也没有问题.

After that, the cross-compile worked cleanly and the newline differences were handled no problem.

我喜欢技术:花了一个多星期的时间来思考我做错了什么,并且交叉编译器已经过时了.哦,对了,我在此期间学到了很多有关C ++的知识,希望对以后的其他人有所帮助.

I love technology: spending over a week thinking I was doing something wrong and the cross-compiler was out of date. Oh well, I learned a lot about C++ in the meantime and hopefully this helps someone else in the future.

再次感谢
R

Thanks again
R

这篇关于C ++交叉编译不处理换行输入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆