This is a simplified version of what i want to do. If i posted the whole thing it would be more than 500 lines so I made this to test stuff out and I get the same error as in the big one.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int node1, node2;
char *cmnd;
char *file;
int arr[1000], arr2[1000];
ifstream commands;
commands.open("commands.txt", ios::in);
if (!commands)
{
cerr<<"failed to open commands.txt"<<endl;
return 1;
}
cout<<"hello";
commands>>cmnd>>file;
cout<<"bye";
ifstream input;
input.open(file, ios::in);
if (!input)
{
cerr<<"failed to open input.txt"<<endl;
return 1;
}
int i = 0;
while(input.good())
{
input>>node1>>node2;
arr[i] = node1;
arr2[i] = node2;
i++;
}
commands>>cmnd;
while (!strcmp(cmnd, "WRITE_INDEX"))
{
commands>>node1>>node2;
if (strcmp(cmnd, "INSERT_LINK"))
{
arr[i] = node1;
arr2[i] = node2;
i++;
}
/*if (strcmp(cmnd, "DELETE_LINK"))
{
//find node 1 in main AVL tree
//delete node 2 from node 1 friends AVL tree
//if node 1 friend pointer is NULL
//delete node 1 from main AVL tree
}*/
commands>>cmnd;
}
commands>>file;
ofstream output;
output.open(file, ios::out);
if (!output)
{
cerr<<"failed to open output.txt"<<endl;
return 1;
}
while (i>0)
{
output<<arr[i]<<" "<<arr2[i]<<endl;
i--;
}
}
Let me explain. The commands.txt is something like this:
READ_DATA input.txt
INSERT_LINK 1 2
INSERT_LINK 5 6
INSERT_LINK 6 7
WRITE_INDEX output.txt
but with more insert or delete link in the middle. It has to start with READ_DATA and end with WRITE_INDEX.
The input.txt looks like this:
34 863
929 174
586 316
892 494
random numbers in 2 columns. I want to save the left column in arr[1000] and the right one in arr2[1000] and then print them in reverse in the output.txt.
When I run the program in code blocks it crashes, so i added these
cout<<hello
and
cout<<bye
in a suspicious area and as it turns out only hello goes on screen. This means the program crashes in
commands>>cmnd>>file;
I can't find what is wrong with the code. Any help will be appreciated.