c++中的分段故障,无输出

Segmantation fault in c++, no output

本文关键字:输出 故障 分段 c++      更新时间:2023-10-16

我正在尝试制作一个C++程序来读取文件中的输入,在分隔符之前放置空格,并写入另一个文件。示例:输入:int main()输出:int main()

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
fstream oup, inp;
int dsize = 18;
char delim[] = {',',  ' ', 'n', 't', '"', '(', ')', '{', '}',
                ''', '[', ']',  '+',  '-',  '*', '&', '/', '%'};
bool isDelim(char c) {
  for (int i = 0; i < 18; i++)
    if (c == delim[i])
      return true;
  return false;
}
void chartost(string a) {
  int d = 0;
  if (a.length() == 1)
    oup << a << " ";
  else {
    for (unsigned i = 0; i < a.length(); i++) {
      if (isDelim(a[i])) {
        d = 1;
        oup << a.substr(0, i) << " ";
        chartost(a.substr(i, a.length()));
      }
    }
    if (d == 0) {
      oup << a << " ";
    }
  }
}
int main() {
  cout << "Initial Point";
  inp.open("test.c", ios::in);
  oup.open("testspace.c", ios::out);
  string a;
  cout << "before isopen";
  if (inp.is_open() && oup.is_open()) {
    while (inp >> a) {
      cout << a;
      chartost(a);
    }
    cout << "after operations n";
    inp.close();
    oup.close();
  }
  return 0;
}

为了调试,我在很多地方都使用了cout。我遇到了一个分段错误,甚至主入口点的cout也没有显示。

代码一团糟,很难发现错误,但我注意到您的chartost函数是递归的,但在递归退出时不会终止。第一个调用的for循环将在代码对字符串的其余部分进行递归后处理字符串的剩余部分。我会从那里开始寻找你的问题。