Segmantation Error when running program

I am writing a program to create a text editor using a main.cpp file and a linked list class called LineList, and I keep hitting a segmentation fault when the marked line in triggered. I don't know what's causing this at all, I've tried multiple things to fix it, so any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  //Main code
  int main(int argc, char* argv[])
  {
	char menuchoice = 'a', quitchoice = 'a';
	int linenumber = 0;
	const int ARGUMENTS = 2;
	LineList linelist;
	if (argc != ARGUMENTS)
	{
		cout << "It needs one command line argument: <input filename>" << endl;
		return 1;
	}

	//Read input file into a LineList
	string line;
	//Input user-directed file
	ifstream myfile(argv[1]);
	if (myfile.is_open())
	{
		//Input current file contents into list, using InsertAtEnd function
		int linenumber = 0;
		while (getline(myfile, line)) //Problem happens around here
		{
			linelist.Insert(line, linenumber);
			//Update linenumber
			linenumber++;
		}
		myfile.close();
	}
	else
	{
		cout << "Unable to open file";
	}
	//Show menu and ask for user choice
	showmenu;
	//Run Print function
	linenumber = 0;
	while (linenumber < (listsize = linelist.GetListSize())
	{
		string printline = linelist.GetLine(linenumber);
		cout << linenumber << "> " << printline << endl;
		linenumber++;
	}

	//Run while user has not chosen to exit program
	while (menuchoice != 'Q')
        //Rest of code

//Relevant LineList code
class LineList
{
	private:
		NewLine* head;
		int listsize;
		string copystring;
	public:
		LineList(); //Constructor for LineList
		~LineList(); //Destructor for LineList
		void Insert(string text, int linenumber); //Insert new line at selected line number

void LineList::Insert(string text, int linenumber)
{
	if (linenumber == 0)
	{
		NewLine* newline = new NewLine(text); //create new node
		newline->next = head;

		head = newline; //Insert new node at front of list
	}

	else if (linenumber > 0 && linenumber < listsize)
	{
		NewLine* cur; //Create variables to traverse list
		NewLine* pre;
		cur = head;
		pre = NULL;
		int i = 0;
		while (cur != NULL) //Go while list is not at end 
		{
			i++;
			if (i == linenumber) break; //Exit if line number is reached
			pre = cur; cur = cur->next;
		}
		NewLine* newline = new NewLine(text); //create new line

		newline->next = cur;
		pre->next = newline; //Place new line in list
	}
	else
	{
		NewLine* newline = new NewLine(text); //create new node
		newline->next = NULL;

		NewLine* cur;
		cur = head;
		while (cur->next != NULL) //get to end of list
		{
			cur = cur->next;
		}
		while (listsize < linenumber)
		{
			NewLine* blankline = new NewLine(""); //create blank lines until desired line is reached
			blankline->next = NULL;

			cur->next = blankline; //insert blank line at end of list
			listsize++; //update list size
		}
		cur->next = newline; //insert new line at end of list
	}
	listsize++; //Increase list size
}


Thanks again for any help!

Edit: I should mention that this program is designed to run through a UNIX terminal and that argv [1] is a text file provided by the user
Last edited on
> while (getline(myfile, line)) //Problem happens around here
I would bet it is two lines below, at linelist.Insert(line, linenumber);


> I've tried multiple things to fix it, so any help is appreciated.
debugger, backtrace, check your pointers.
valgrind, check invalid access.

If you could provide enough code so we test it on our own end, that would be great.


> argv [1] is a text file provided by the user
¿format? ¿content?
All the code necessary to completely run the program would probably make it a very long forum post, so I put everything in a drive folder to save space here. Included is a readme with instructions on how to compile and the format for doing so

https://drive.google.com/drive/folders/0ByLSY8auWcJdYTVMZHhQdmF3elE?usp=sharing
Topic archived. No new replies allowed.