The code runs perfectly, but when I press any button, for continuing the system Pause, it gives the error:

Run-Time Check Failure #2 - Stack around the variable 'arq_linha_part' was corrupted.

#include "stdafx.h"
#include "Sistema.h"

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib> 

#define ZB 100.0
#define PB 100.0
int _tmain(int argc, _TCHAR* argv[])

{
	int system_entry,counter=0;
/*ENTRADA DE DADOS*/
	std::ifstream arquivo("c:\\entrada.txt");

    if (!arquivo) 
    {
        std::cout << "File not found\n\n";
        std::system("PAUSE");
        std::exit(EXIT_FAILURE);
    }
    else
    {
        std::cout << "Loading file...Please wait...\n\n";
    }

    std::string arquivo_linha, arq_linha_part;
	double parm[4];
	//char * cstr;

    while (getline(arquivo, arquivo_linha))
    {
        if (arquivo_linha[0] != '#')
        {
				switch (arquivo_linha[0])
				{
				case 'B':
				system_entry = 1;
				break;
				case 'Z':
				system_entry = 2;
				break;
				case 'S':
				system_entry = 3;
				break;
				default:
				std::cout << "Unkown command, exiting program" << std::endl << std::endl;
				std::system("PAUSE");
				exit(0);
				break;
				}//end switch
			
			if (system_entry == 1)
			{counter =0 ;
				std::istringstream token(arquivo_linha);
				while(std::getline(token, arq_linha_part, ':'))
				{
				std::stringstream temp(arq_linha_part);
					if (counter != 0){
					 if (counter == 1)
					 {
					 temp >> parm[0];
				     std::cout << "Parm 0: " << parm[0] << std::endl;
					 } 
					 else 
					 if (counter ==2){/*passa para string*/}
					 else 
					 if (counter == 3)
					 {
					 temp >> parm[1];
				     std::cout << "Parm 1: " << parm[1] << std::endl;
					 } 
					 else 
					 if (counter == 4)
					 {
					 temp >> parm[2];
				     std::cout << "Parm 2: " << parm[2] << std::endl;
					 } 
					 else
					 if (counter == 5)
					 {
					 temp >> parm[3];
				     std::cout << "Parm 0: " << parm[3] << std::endl;
					 } 
					 else
					 if (counter == 6)
					 {
					 temp >> parm[4];
				     std::cout << "Parm 0: " << parm[4] << std::endl;
					 }
					}//end if !=0
					counter++;
				}//while end
			}//end system 1
			else if (system_entry == 2)
			{
				std::cout << "Z da barra" << std::endl;
			}//end system 2
			else if (system_entry == 3)
			{
				std::cout << "Shunt" << std::endl;
			} //end system 3
			else {
				std::cout << "internal error! exiting program" << std::endl;
				std::system("PAUSE");
			}
        }//fim do if '#'
	}//fim while
	std::cout << "Finished reading file" << std::endl;
std::system("PAUSE"); 
//the run time error happens here... afeter the system pause
return 0;
}

Thanks,

Fernando

Recommended Answers

All 3 Replies

You need to break things into functions so its more readable. Also try this :

while (arquivo && getline(arquivo, arquivo_linha))

The error was being caused because the double vector is limited with four positions, so it doesn´t have the null terminator,
after declarint

double parm[5]; it worked fine =)

Thanks!

Fernando

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.