Streams in C++ crashing the programme -


This is a simplified version of what I want to do. If I post the whole thing then there will be more than 500 lines, so I created it to test the material and I get an error similar to that of a big mistake.

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Cstring & gt; using namespace std; Int main () {int node1, node2; Four * CMD; Four * file; Int ARR [1000], AR2 [1000]; Easiest command; Commands.open ("commands.txt", iOS :: In); If (! Command) {cerr & lt; & Lt; "failed to open commands.txt" & lt; & Lt; Endl; Return 1; } Cout & lt; & Lt; "Hello"; Orders & gt; & Gt; Cmnd & gt; & Gt; File; The court's & lt; & Lt; "Bye"; Instream input; Input.open (file, iOS :: in); If (! Input) {cerr & lt; & Lt; "Failed to open input"; & Lt; & Lt; Endl; Return 1; } Int i = 0; While (input.good ()) {Input> & Gt; Node 1 & gt; & Gt; Node2; arr [i] = node 1; Arr2 [i] = node2; I ++; } Order & gt; & Gt; CMD; While (! Strcmp (cmnd, "WRITE_INDEX")) {commands & gt; Node 1 & gt; & Gt; Node2; If (SRCMP (CMMD, "INSERT_LINK")) {arr [i] = node 1; arr2 [i] = node2; I ++; } / * If (SRMMP (CMMD, "DELETEX")) // Find node 1 in main AVL tree / node 1 from friends AVL tree / if node 1 meter pointer NULL // Delete node 1 to main AVL tree } * / Order & gt; & Gt; CMD; } Order & gt; & Gt; File; Offstream output; Output.open (file, iOS :: out); If (! Output) {cerr & lt; & Lt; "Failed to open output.txt" & lt; & Lt; Endl; Return 1; } While (i & gt; 0) {output   

Let me explain the command.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 enter more in the middle or remove link, it must start with READ_DATA and WRITE_INDEX's Will end up with

The input tint looks like this:

34 863

929 174

586 316

892 494

Random numbers in 2 columns I want to save the right one in the ARR [1000] and the correct one in AR2 [1] and then output.tt I want to print them in reverse.

When I crash the program into the code block, I get it

  cout & lt; & Lt; Hello   

and

  cout & lt; & Lt; Bye   

In a suspicious area and as it goes to the Hello screen only this means that

  command & gt; & Gt; CMD & gt; & Gt; The program crashes in the file;   

Any help I'm not getting wrong with the code will be appreciated.

It's writing at random place in memory:

  commands & Gt; & Gt; cmnd & gt; & Gt; File;  cmnd  as the   

(and file ) is an introductory indicator instead of using char * And immediately check the outcome of the IO operation (do not use (see why input.good ()), :

  std :: string cmnd; Std :: string file; If  strcmp  can be changed in the following way:  
   code> if (CMD == "WRITE_INDEX") {}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -