- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道这与 Windows 和 Linux 中的行尾指示符之间的差异有关,但我不知道如何解决它。
我确实看过这个帖子 Getting std :: ifstream to handle LF, CR, and CRLF?但是当我使用该帖子中的简化版本时(我使用直接读取而不是缓冲读取,知道存在性能损失,但现在想保持简单),它并没有解决我的问题,所以我希望在这里得到一些指导。我确实测试了帖子的修改版本,它成功地找到并替换了我暂时用于测试场景的字符和选项卡,因此逻辑正常,但我仍然遇到问题。
我知道我在这里遗漏了一些非常基本的东西,当有人帮助我解决这个问题时,我可能会感到非常愚蠢,所以我宁愿不公开承认我的愚蠢,但我已经为此工作了一个星期,但无法解决它,所以我正在寻求帮助。
我是 C++ 新手,所以如果我在这里做的事情真的很菜鸟,请在回答时保持温和:-)
我创建了以下单文件程序来原型(prototype)化我想要做的事情。这是一个简单的例子,但我需要让它发挥作用才能更进一步。这不是一个家庭作业问题;而是一个问题。我真的需要解决这个问题才能创建一个应用程序。
程序(如下所示):
编译时没有错误或警告,并且在 CentOS 机器上干净地运行;
在 CentOS 机器上使用 mingw32 进行交叉编译,没有错误或警告,并且在 Windows 上干净地运行;
所以,是的,它与 Linux 和 Windows 之间的不同文件格式有关,并且可能与换行代码有关,但我试图适应这一点,但它不起作用。
更复杂的是,我发现旧的 Mac 换行符又不同了:
.
请帮忙! ...
.
我想要:
所以我需要检查文件,确定正在使用的换行符并进行相应处理
有什么建议吗?
我当前的(简化的)代码(尚未进行验证检查)是:
[代码]
int main(int argc, char** argv)
{
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() );
bool failure_flag = false;
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;
failure_flag = true;
}
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;
failure_flag = true;
}
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 int file_line_length = file_line.length() +1 ;
long int char_num = 0;
for ( char_num = 0 ; char_num < file_line_length ; char_num++ )
{
if ( file_line[ char_num ] == '\n' )
{
if ( char_num == file_line_length - 1 )
{
file_line[ char_num ] = '-';
}
else
{
if ( file_line[ char_num + 1 ] == '\n' )
{
file_line[ char_num ] = ' ';
}
else
{
file_line[ char_num ] = ' ';
}
}
}
}
int field_display_width = 4;
std::cout << "Line " << std::setw( field_display_width ) << line_num <<
", starting at character position " << std::setw( field_display_width ) << starting_char_pos <<
", contains " << file_line << "." << std::endl;
starting_char_pos = rc_input_file_holder.tellg();
rc_output_file_holder << "Line " << line_num << ": " << file_line << std::endl;
}
rc_input_file_holder.close();
rc_output_file_holder.close();
delete [] RC_INPUT_FILE_NAME;
delete [] RC_OUTPUT_FILE_NAME;
}
}
if ( failure_flag )
{
return EXIT_FAILURE;
}
else
{
return EXIT_SUCCESS;
}
}
[/代码]
相同的代码,有很多注释(为了我的学习经验)是:
[代码]
/*
* The main function, from which all else is accessed
*/
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
*/
// Set the filenames to be used in this file-handling program
std::string rc_input_file_name = "rc_input_file.txt";
std::string rc_output_file_name = "rc_output_file.txt";
// Note that when the filenames are used in the .open statements below
// they have to be in a cstring format, not a string format
// so the conversion is done here once
// Use the Capitalized form of the file name to indicate the converted value
// (remember, variable names are case-sensitive in C/C++ so NAME is different than name)
// This conversion could be done 3 ways:
// - done each time the cstring is needed:
// file_holder_name.open( string_file_name.c_str() )
// - done once and referred to each time
// simple method:
// const char * converted_file_name = string_file_name.c_str()
// explicit method (2-step):
// char * converted_file_name = new char[ string_file_name.length() + 1 ];
// strcpy( converted_file_name, string_file_name.c_str() );
// This program uses the explicit method to do it once for each filename
// because by doing so, the char array created has variable length
// and you do not risk buffer overflow
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() );
// This will be set to true if either the input or output file cannot be opened
bool failure_flag = false;
// Open the input file
std::ifstream rc_input_file_holder;
rc_input_file_holder.open( RC_INPUT_FILE_NAME , std::ios::in );
// Validate that the input file was properly opened/created
// If not, set failure flag
if ( ! rc_input_file_holder.is_open() )
{
// Could not open the input file; set failure flag to true
std::cout << "Error - Could not open the input file" << std::endl;
failure_flag = true;
}
else
{
// Open the output file
// Create one if none previously existed
// Erase the contents if it already existed
std::ofstream rc_output_file_holder;
rc_output_file_holder.open( RC_OUTPUT_FILE_NAME , std::ios::out | std::ios::trunc );
// Validate that the output file was properly opened/created
// If not, set failure flag
if ( ! rc_output_file_holder.is_open() )
{
// Could not open the output file; set failure flag to true
std::cout << "Error - Could not open or create the output file" << std::endl;
failure_flag = true;
}
else
{
// Get the current position where the character pointer is at
// Get it before the getline is executed so it gives you where the current line starts
std::streampos char_num = 0;
// Initialize the line_number and starting character position to 0
long int line_num = 0;
long int starting_char_pos = 0;
std::string file_line = "";
while ( getline( rc_input_file_holder , file_line ) )
{
// Set the line number counter to the current line (first line is Line 1, not 0)
line_num = line_num + 1;
// Check if the new line designator uses the standard for:
// - linux (\n)
// - Windows (\r\n)
// - Old Mac (\r)
// Convert any non-linux new line designator to linux new line designator (\n)
long int file_line_length = file_line.length() +1 ;
long int char_num = 0;
for ( char_num = 0 ; char_num < file_line_length ; char_num++ )
{
// If a \r character is found, decide what to do with it
if ( file_line[ char_num ] == '\n' )
{
// If the \r char is the last line character (before the null terminator)
// the file use the old Mac format to indicate new line
// so replace the \r with \n
if ( char_num == file_line_length - 1 )
{
file_line[ char_num ] = '-';
}
else
// If the \r char is NOT the last line character (before the null terminator)
{
// If the next character is a \n, the file uses the Windows format to indicate new line
// so replace the \r with space
if ( file_line[ char_num + 1 ] == '\n' )
{
file_line[ char_num ] = ' ';
}
// If the next char is NOT a \n (and the pointer is NOT at the last line character)
// then for some reason, there is a \r in the interior of the string
// At this point, I do not know why this would be
// but I don't want it left there, so replace it with a space
// Yes, I know this is the same as the above action,
// but I left is separate to allow for future flexibility
else
{
file_line[ char_num ] = '-';
}
}
}
}
// Output the contents of the line just fetched
// This is done in this prototype file as a placeholder
// In the real program, this is where the validation check(s) for the line would occur)
// and would likely be done in a function or class
// The setw() function requires #include <iomanip>
int field_display_width = 4;
std::cout << "Line " << std::setw( field_display_width ) << line_num <<
", starting at character position " << std::setw( field_display_width ) << starting_char_pos <<
", contains " << file_line << "." << std::endl;
// Reset the character pointer to the end of this line => start of next line
starting_char_pos = rc_input_file_holder.tellg();
// Output the (edited) contents of the line just fetched
// This is done in this prototype file as a placeholder
// In the real program, this is where the results of the validation checks would be recorded
// You could put this in an if statement and record nothing if the line was valid
rc_output_file_holder << "Line " << line_num << ": " << file_line << std::endl;
}
// Clean up by:
// - closing the files that were opened (input and output)
// - deleting the character arrays created
rc_input_file_holder.close();
rc_output_file_holder.close();
delete [] RC_INPUT_FILE_NAME;
delete [] RC_OUTPUT_FILE_NAME;
}
}
// Check to see if all operations have successfully completed
// If so exit this program with success indicated
// If not,exit this program with failure indicated
if ( failure_flag )
{
return EXIT_FAILURE;
}
else
{
return EXIT_SUCCESS;
}
}
[/代码]
我拥有所有正确的包含内容,并且在针对 Linux 进行编译或针对 Windows 进行交叉编译时没有生成任何错误或警告。
我使用的输入文件只有 5 行(愚蠢的)文本:
A new beginning
just in case
the file was corrupted
and the darn program was working fine ...
at least it was on linux
Linux 上的输出正如预期的那样:
Line 1, starting at character position 0, contains A new beginning.
Line 2, starting at character position 16, contains just in case.
Line 3, starting at character position 29, contains the file was corrupted.
Line 4, starting at character position 52, contains and the darn program was working fine ....
Line 5, starting at character position 94, contains at least it was on linux.
当我导入在 Linux 中创建的文本文件时,Windows 中的输出是相同的,但是当我使用记事本并在 Windows 中手动重新创建相同的文件时,输出为
Line 1, starting at character position 0, contains A new beginning.
Line 2, starting at character position 20, contains t in case.
Line 3, starting at character position 33, contains e file was corrupted.
Line 4, starting at character position 56, contains nd the darn program was working fine ....
Line 5, starting at character position 98, contains at least it was on linux.
注意第 2、3、4 和 5 行起始字符位置的差异注意第 2,3 和 4 行开头缺少的字符
欢迎任何和所有的想法......
最佳答案
查看分辨率
为了解决这个问题,通过 apt-get install 安装的 mingw 交叉编译器已经过时了。当我手动安装更新的交叉编译器并更新设置以防止出现一些错误消息时,一切正常。
关于C++ 从文件读取行时截断字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681515/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!