无法打开使用ofstream创建的文件

时间:2012-10-06 17:30:02

标签: c++ ofstream

无法在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;

}

1 个答案:

答案 0 :(得分:2)

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

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