Can anyone help me figure out why I have a segmentation fault error occurring during runtime? I'm guessing that I am accessing memory that doesn't exist or attempting to place data into a node that hasn't been allocated, but can not figure it out.
FYI - It's a work in progress, haven't written to the outfile yet, cleaned up unnecessary variables etc - but this segmentation fault issue is my biggest nemisis. The Node class constructor just initializes a string data variable and a null pointer.
#include <iostream>
#include <iomanip>
#include <fstream> // provides file handling
#include <cassert> // provides file assertions
#include <string> // provides string manipulation
#include "node1.h" // provides node class
using namespace std;
using namespace my_linked_list;
int main ()
{
int i = 0;
string revfile;
string tempstr;
string readystr;
string c;
node *begin_ptr;
node *cursor;
ifstream infile;
ofstream outfile;
cout << "What is the name of the text file you want to see in reverse order? ";
cin >> revfile;
cout << endl;
infile.open(revfile.c_str());
assert(infile);
while (infile) {
while (getline(infile, tempstr)) {
for (i=0; i<tempstr.length() ; i++){
if (tempstr[i] == ' ' && tempstr[i+1] == ' ') {
infile.ignore( );
}
else if (tempstr[i] == ' ') {
c = tempstr[i];
readystr = readystr + c;
begin_ptr = new node(readystr,begin_ptr);
}
else {
c = tempstr[i];
readystr = readystr + c;
}
}
if (readystr.length() > 0){
begin_ptr = new node(readystr,begin_ptr);
readystr.erase(0, readystr.length());
}
begin_ptr = new node("add_a_newline_in_output",begin_ptr);
}
}
infile.close( );
outfile.open("output.txt");
cout << "did we make it here?";
for (cursor = begin_ptr ; cursor != NULL ; cursor = cursor->link())
cout << cursor->data();
return 0;
}