我创建了这个功能。在txt目录中我有txt文件。我尝试循环目录并打开txt文件进行编辑。当行为空时,应该用包含出现次数的文本替换为空行。
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <dirent.h>
#include <boost/algorithm/string/replace.hpp>
int main () {
std::stringstream stream;
std::string content;
std::string path = "U:\\C\\NBK\\txt";
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path.c_str() )) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
int c = 0;
std::stringstream cStr;
printf ("%s\n", ent->d_name);
std::string tmp = path + "\\" + ent->d_name;
std::ifstream ifs( tmp.c_str() );
std::string line;
if (ifs.is_open())
while ( ifs.good() )
{
getline (ifs, line);
if ( line.length() == 0) {
c++;
cStr << c;
line = line + "<h3>" + cStr.str() + "<h3>";
}
std::cout << line << std::endl;
} // end while getline
else
std::cout << "Unable to open file\n";
} // end while readdir
closedir (dir);
}
else
{
perror ("File open failed");
return 0;
}
return 0;
}
我可以从菜单Build:Run中运行程序。问题是当我尝试调试它时。由于SIGSEGV,程序在Code :: Blocks,C ++ 98中崩溃。停在以下部分的第二行:
if ((dir = opendir (path.c_str() )) != NULL) {
while ((ent = readdir (dir)) != NULL) {
构建目标被选为Debug。
请解释这里发生了什么以及如何修复它来调试它。