0

无法在 Ubuntu 11.10 上的 gedit 编辑器中打开 .cpp 文件。该文件是在我用 C++ 编写的程序中使用 ofstream 对象创建的。程序编译并运行没有任何错误,之后它打开一个文件,逐行读取(将每一行附加到 std::string 对象)直到到达末尾,然后将字符串返回给 main(),然后将其输出到它在硬盘驱动器上创建的单独文件。但是,当尝试在 gedit 中打开和显示文件时,编辑器只是挂起,无论我等待多长时间都不会显示任何文本,如果等待时间过长,当我尝试关闭它时,它会说它没有响应并且我必须强制退出它。在 nautilus 文件浏览器中文件的预览图标中,它显示输出文件只有一行文本,而输入文件当然会显示整个页面都充满了文本。情况并非如此,因为输出文件应该包含与输入文件相同的行数,唯一的区别是更改了几行文本。

有人知道是什么原因造成的吗?

请注意,输入文件很长,包含 136871 行代码,所以可能与它有关。

以下是创建输出文件的程序代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string fixIssue_SegmentationFault(ifstream& file_handle);

int main() {

cout<< "Thank you for running FixC_html4_tagsIssues.\n"
       "This little program has been written with the intent of fixing\n"
       "the issues in the C_html4_tags.h file of the MAW programming project.\n"
    << endl
    << "Please hit Enter to fix the detected issues." <<endl;
cin.get();

cout<< "Thank you. Please wait as it fixes the issues..." <<endl;
ifstream C_html4_tags_file_handle("/home/mobilexman/Documents/Programming_Projects/MAW/Modules/core/WebCoder_mod/modules/Html_coder/C_html4_coder/C_html4_elements/C_html4_tags/C_html4_tags.cpp");
string output_str = fixIssue_SegmentationFault(C_html4_tags_file_handle);
C_html4_tags_file_handle.close();

ofstream C_html4_tags_replacement_file_handle("/home/mobilexman/Documents/Programming_Projects/MAW/Modules/core/WebCoder_mod/modules/Html_coder/C_html4_coder/C_html4_elements/C_html4_tags/C_html4_tags_replacement.cpp");
C_html4_tags_replacement_file_handle<< output_str.c_str();
C_html4_tags_replacement_file_handle.close();

cout<< "Congratulations. The issues have been fixed." <<endl;

return 0;

}

string fixIssue_SegmentationFault(ifstream& file_handle) {

//////////////////
//Cause of issue:
/////////////////
///////////////////////////////////////////////////////////////////
//map<S_browser, TYPE_attr_values> attr_supported_attr_values_map;
//...
//TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
//...
//attr_supported_attr_values.clear();
//attr_supported_attr_values_map.clear();
//...
//attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
/////////////////////////////////////////////////////////////////////////////
//Explanation:
//Inside constructor of C_html4_tags, I created a map for the supported attr values of each Html attribute I created for each Html tag in the constructor.
//The map uses an S_browser object for its key and an object of TYPE_attr_values (which is a typedef of std::vector<S_html_attr_value>) for its mapped value.
//Therefore, I created a TYPE_attr_values& called 'attr_supported_attr_values' to reference the associated mapped value vector of the 'dummy_browser' object,
//which is just a dummy browser object I created so I could create a list of standard Html 4.01 attributes which exist in the specification for now, and later
//create a list of attributes from that list for each browser, according to which attribute values each browser supports (note: this will be done at the
//next level up: C_html4_elements). The code line
////////////////////////////////////////////////////////////////////////////////////////////////
//TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
///////////////////////////////////////////////////////////////////////////////////////////////
//is where I first create that reference and initialize it to the returned reference of the [] operator of the map, which performs an insert operation if the key passed does not already exist inside the map.
//And the code line
//////////////////////////////////////////////////////////////////////////////
//attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
/////////////////////////////////////////////////////////////////////////////
//is where I reinitialize (or at least that's what I thought it did when I first wrote the code...) the reference to the returned reference again, for each Html attribute after the first attribute.
//I then attempt to clear after each attribute operations both the referenced vector and then the map before reusing the reference 'attr_supported_attr_values'.
//However, that is what created the segmentation fault. It turns out you cannot use the same reference on more than one object, unlike pointers. So it is invalid to reinitialize that reference
//to point to a new object for each attribute, thus creating the problem. Oddly enough, though, doing this didn't actually produce any compiler errors. It compiles ok, but when attempting to run the program,
//it will return a message saying the program unexpectedly exited when running it normal, and you only get the extra info about the segmentation fault when running the debugger on it.  I'm not sure why
//that is, but its probably by design. And so it did take me a bit to figure out why the 'attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];' lines was producing the segmentation
//fault, especially since the attribute it ended on was not actually the second attribute of the first tag, but rather the first attribute of the second tag. I still don't know why that is, but at any rate,
//I believe that after I change the 'attr_supported_attr_values' identifier to a pointer instead of a reference, and adjust all lines using it to treat it as a pointer instead of a reference, the problem
//SHOULD be fixed. But we'll certainly see how it goes...

//////////////////////////
//Steps for fixing issue:
////////////////////////
//1. Read each line of C_html4_tags.cpp until reaching the end, checking each line as we go to see if it contains the target string content. In addition, add the content of each line (regardless of whether it contains the target or not)
//to output_str. Note that the target string content is first "TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];" which needs to be changed to
//"TYPE_attr_values* attr_supported_attr_values = &attr_supported_attr_values_map[dummy_browser];", and then is the
/////////////////////////////////////
//attr_supported_attr_values.clear();
//attr_supported_attr_values_map.clear();
/////////////////////////////////////////
//lines which needs to be changed to
///////////////////////////////////////
//attr_supported_attr_values->clear();
//attr_supported_attr_values_map->clear();
//////////////////////////////////////////
//2. If the target is found in the current line, perform the necessary changes to the line's content (i.e. buffer_str) before adding it to output_str.
//3. Return output_str.
string output_str;
string buffer_str;
string search_str1 = "TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];";
string replacement_str1 = "TYPE_attr_values* attr_supported_attr_values = &attr_supported_attr_values_map[dummy_browser];";
string search_str2 = "attr_supported_attr_values.clear();";
string replacement_str2 = "attr_supported_attr_values->clear();";
string search_str3 = "attr_supported_attr_values_map.clear();";
string replacement_str3 = "attr_supported_attr_values_map->clear();";
size_t pos;
while (getline(file_handle, buffer_str)) {
     if (file_handle.good()) { //i.e. no errors while reading lines
        if ((pos = buffer_str.find(search_str1, 0)) != string::npos) {
           buffer_str.replace(pos, search_str1.size(), replacement_str1);
        }

        else if ((pos = buffer_str.find(search_str2, 0)) != string::npos) {
           buffer_str.replace(pos, search_str2.size(), replacement_str2);
        }

        else if ((pos = buffer_str.find(search_str3, 0)) != string::npos) {
           buffer_str.replace(pos, search_str3.size(), replacement_str3);
        }
        output_str += buffer_str;
     }
}

return output_str;

}
4

1 回答 1

2

您的问题是getline 提取并丢弃行终止符,这意味着您最终在输出中没有一个换行符。这很可能会让 GEdit 非常不高兴(导致您看到的行为),因为它必须计算一个巨大文件的换行。

因此,您应该在构建输出字符串时为每一行添加一个换行符。

于 2012-10-06T17:34:11.647 回答