Ubuntu 11.10 の gedit エディターで .cpp ファイルを開くことができません。このファイルは、私が C++ で書いたプログラムで ofstream オブジェクトを使用して作成されました。プログラムはエラーなしでコンパイルおよび実行されます。その後、ファイルを開き、最後に到達するまで 1 行ずつ読み取り (各行を std::string オブジェクトに追加します)、次に文字列を main() に返します。ハードドライブ上に作成する別のファイルに出力します。ただし、gedit でファイルを開いて表示しようとすると、エディターがハングするだけで、どれだけ待ってもテキストが表示されません。強制終了しなければなりません。nautilus ファイル ブラウザのファイルのプレビュー アイコンでは、出力ファイルが 1 行のテキストのみで表示されます。もちろん、入力ファイルにはページ全体がテキストで埋められています。出力ファイルには入力ファイルと同じ行数が含まれているはずなので、そうではありません。相違点は、数行のテキストが変更されただけです。
誰がこれを引き起こしているのか考えていますか??
入力ファイルは非常に長く、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;
}