Memory exception error in Strings

Hi, I am trying to retrieve data from the CSV file. I am creating linked list to copy that data.
temp = (Signal_Config*)malloc(sizeof(Signal_Config)); // allocate space

i cant alllocte memory for this. because i am using that members as string in struct.(please see the header file). i tired to initialize members as char pointer, i cant covert that string data to char pointer.(i tired to use c_str)

i am beginner. i want to save these string data. Please help.

CSV file data:

Vehicle_Type: Laguna
FAILURE_TYPE Signal_Discription CAN_ID Start_Bit Bit_Length Bit_Value CAN_ID_Discription
FlashingIndicators 0x766 60 2 3 UCH_base
1 EngineControlFailureLevel2 0x77A 54 1 1 INJ_base_lente
3 WarningWaterTemp 0x77A 27 1 1 INJ_base_lente
4 FuelLow 0x711 5 1 1 TdB_Base







CODE:

#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <sstream>
#include <stdio.h>
#include<conio.h>
#include "CSV_RW.h"

using namespace std;

int main()
{
string input_file_name = "D:\\LocalData\\Desktop\\test\\Configuration_file.csv";


Signal_Config *head = NULL; //empty linked list


ifstream input_stream(input_file_name.c_str());

string buffer;
int rows = 0;
int data_avail = 0;
int set=0;
int Number_of_Signals = 0;

string delimiter = ",";
size_t pos = 0;
string Buffer_Data[7];
string Failure_Type_FL = "";
string Signal_discription_FL = "FlashingIndicators";

while(!input_stream.eof())
{
getline(input_stream,buffer,'\n');
string Discription_list = buffer;

if(data_avail == 0)
{
for (int i=0;((pos = buffer.find(delimiter)) != std::string::npos); i++)
{
Buffer_Data[i] = buffer.substr(0, pos);
//std::cout << Buffer_Data[i] << " ";
buffer.erase(0, pos + delimiter.length());
}

if (Buffer_Data[0] == "Vehicle_Type:")
{
Vehicle_Type = Buffer_Data[1];
}
}





if (data_avail==1)
{
for (int i=0;((pos = buffer.find(delimiter)) != std::string::npos); i++)
{
Buffer_Data[i] = buffer.substr(0, pos);
//std::cout << Buffer_Data[i] << " ";
buffer.erase(0, pos + delimiter.length());
}
if (Failure_Type_FL == Buffer_Data[0] && Signal_discription_FL == Buffer_Data[1])
{
FL_CAN_ID = Buffer_Data[2];
FL_Start_Bit = Buffer_Data[3];
FL_Bit_Length = Buffer_Data[4];
FL_Bit_Value = Buffer_Data[5];
FL_CAN_ID_Discription = Buffer_Data[6];

}
else
{
Signal_Config *temp; // create a temporary
temp = (Signal_Config*)malloc(sizeof(Signal_Config)); // allocate space
temp->FAILURE_TYPE = Buffer_Data[0];
temp->Signal_Discription = Buffer_Data[1];
temp->CAN_ID = Buffer_Data[2];
temp->Start_Bit = Buffer_Data[3];
temp->Bit_Length = Buffer_Data[4];
temp->Bit_Value = Buffer_Data[5];
temp->CAN_ID_Discription = Buffer_Data[6];
temp->next=head; // store the address of the pointer head(second field)
head = temp;
Number_of_Signals++;
}

}

if (Discription_list == config_list)
{
data_avail = 1;
}

}


temp1 = head;

while( temp1!=NULL )
{

cout<< temp1->FAILURE_TYPE<<" ";
cout<< temp1->Signal_Discription<<" ";
cout<< temp1->CAN_ID<<" "; // show the data in the linked list
cout<< temp1->Start_Bit<<" ";
cout<< temp1->Bit_Length<<" ";
cout<< temp1->Bit_Value<<" ";
cout<< temp1->CAN_ID_Discription<<" ";
cout<< "\n";
temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp'
}

cout<<endl<<"Number of Signals: "<<Number_of_Signals<<endl;



input_stream.close();
getchar();
return 0;

}


Header File

#include <string>
#include<iostream>

#define __CSV_RW_H
using namespace std;

string config_list = "FAILURE_TYPE,Signal_Discription,CAN_ID,Start_Bit,Bit_Length,Bit_Value,CAN_ID_Discription";
string FL_CAN_ID;
string FL_Start_Bit;
string FL_Bit_Length;
string FL_Bit_Value;
string FL_CAN_ID_Discription;
string Vehicle_Type;

typedef struct Signal_Config
{
string FAILURE_TYPE;
string Signal_Discription;
string CAN_ID;
string Start_Bit;
string Bit_Length;
string Bit_Value;
string CAN_ID_Discription;
Signal_Config *next;
};
Last edited on
Did you try using new, which is the C++ way of allocating memory, rather than malloc.

temp = new Signal_Config;

I was trying to understand the CSV data, but there were no comma separators in the sample data shown above. I was also wondering, is that the entire file, or will it contain data for multiple vehicles. Also do you have to use a linked list, wouldn't an array or vector do?
Last edited on
Topic archived. No new replies allowed.