1

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.

The Gramm
  • 118
  • 2
  • 12

2 Answers2

4

This is writing to a random location in memory:

commands>>cmnd>>file;

as cmnd (and file) is an uninitialized pointer. Use std::string instead of char* and check the result of IO operations immediately (don't use while(input.good()), see Why is “while ( !feof (file) )” always wrong? for why):

std::string cmnd;
std::string file;

if (commands >> cmnd >> file)
{
}

The == operator is overloaded for std::string so strcmp can be replaced with:

if (cmnd == "WRITE_INDEX")
{
}
Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thanks a lot, it worked! But i still use good(). I don't understand what you mean with check the result of IO operations immediately. But i guess as long as it runs i am good to go :D Oh also to make it work in input.open(file, ios::in) in needed to replace file with "input.txt" since it says open needs a constant char * in order to work. – The Gramm Apr 22 '14 at 13:50
  • @TheGramm, if you don't check the result of IO operations immediately then subsequent code will use stale or unpopulated variables. A common manifestation of this is to process the last line of an input file twice. Though the linked question in this answer refers to `feof(FILE*)` the same can be applied to streams. – hmjd Apr 22 '14 at 13:51
2

You have:

char *cmnd;
char *file;

And then you do:

commands >> cmnd;
while (!strcmp(cmnd, "WRITE_INDEX"))

The first command fails leaving cmnd uninitialized and then strcmp(cmnd, ...) probably crashes it.

Replace the above declarations with:

std::string cmnd, file;
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271