Hello! As the title says when i run my code the program crashes if not running in it through Microsoft Visual C++.
When i try to run the file normally(double-click) it prints my debugmessages up to "ReadElement5 result.size = " and there it crashes, it prints ReadElement5 result.size = but not the value of the string::size.
Im making a half-crappy Ini-write/read for my program... Here is the code:
Code://Ini.h #include <iostream> #include <Windows.h> using namespace std; class Ini { int cursor, eof, MAX_INI_LENGTH; string szIniData; FILE *fFile; public: Ini(); bool Select(char *parent); bool Read(); void Clear(); char *ReadElement(char *element); bool Write(char *element, char *value); bool WriteParent(char *parent); bool Open(char *file); bool Close(); };
Code://Ini.cpp #include "Ini.h" Ini::Ini(){ MAX_INI_LENGTH = 1024*4; eof = 0; cursor = 0; //szIniData = ""; } bool Ini::Open(char *file){ if(fFile = fopen(file, "r+")){ if(fFile != NULL){ return true; } } return false; } bool Ini::Select(char *parent){ char *formattedParent = new char[strlen(parent)+2]; ZeroMemory(formattedParent, strlen(parent)+2); cout << "Select1\n"; sprintf(formattedParent, "[%s]\r\n", parent);//this defines a parent, then next \r\n is from element cout << "Select2\n"; //cout << "Comparing: " << szIniData.c_str() << "!!! to " << formattedParent << "!!!\n"; if(szIniData.find(formattedParent) != szIniData.npos){ cout << "Select3\n"; if(cursor != 0) cursor = szIniData.find(formattedParent) + strlen(parent) + 8 + 1;//8 byte = \r\n\r\n[]\r\n and then the next byte as target(+1) else cursor = szIniData.find(formattedParent) + strlen(parent) + 4 + 1;//4 byte = [] and \r\n and then the next byte as target(+1) cout << "Select4\n"; //delete formattedParent; return true; } cout << "Select5(error)\n"; //delete formattedParent; return false; } bool Ini::Read(){ long lSize; cout << "Read1\n"; fseek(fFile, 0, SEEK_END); cout << "Read2\n"; lSize = ftell(fFile); cout << "Read3\n"; rewind(fFile); cout << "Read4\n"; if(lSize < MAX_INI_LENGTH){ char *buffer = new char[lSize]; fread(buffer, 1, lSize, fFile); szIniData = buffer; //delete buffer; return true; } cout << "Read5(error)\n"; return false; } char *Ini::ReadElement(char *element){ int pointer = 0, offset = 0, nextElement = 0; cout << "ReadElement1\n"; if(szIniData.find(element) == szIniData.npos) return NULL; if((char) szIniData[szIniData.find(element)-1] != '\n'){ cout << "ReadElement2(Special Path)\n"; string part; part.assign(szIniData); cout << "Char before Pass is " << (char) part[part.find(element)-1] << "!!!"; while((char) part[part.find(element)-1] != '\n'){ //pointer = szIniData.find(element+offset) + strlen(element) + 3;//the +3 are the the <SP>=<SP> int partSize = part.size(); offset += strlen(element); part.assign(part.substr((int)part.find(element) + offset, partSize-((int) element))); nextElement = part.find(element); pointer = szIniData.find(element)+1 + nextElement + strlen(element) + 6; } }else{ cout << "ReadElement3(After special)\n"; pointer = szIniData.find(element) + strlen(element) + 3;//the +3 are the the <SP>=<SP> } int pointer2 = szIniData.find("\r\n", pointer); string result; result.assign(szIniData.substr(pointer, pointer2-pointer)); cout << "ReadElement4\n"; cout << result.c_str() << endl; cout << result.c_str() << endl; cout << result.c_str() << endl; cout << "ReadElement5 result.size = " << result.size() << "\n"; char *cElement = new char[result.size()]; cout << "ReadElement6\n"; strcpy(cElement, result.c_str()); cout << "ReadElement7\n"; getchar(); return cElement; } bool Ini::Write(char *element, char *value){ cout << "Write1\n"; fseek(fFile, cursor, SEEK_SET); cout << "Write2\n"; fputs("\r\n", fFile); fputs(element, fFile); fputs(" = ", fFile); fputs(value, fFile); cout << "Write3\n"; cursor += strlen(element) + strlen(value) + 6; cout << "Write4\n"; return true; } bool Ini::WriteParent(char *parent){ fseek(fFile, cursor, SEEK_SET); if(cursor!=0) fputs("\r\n\r\n", fFile);//+4bytes fputs("[", fFile);//+1byte fputs(parent, fFile);//+11 byte fputs("]", fFile);//+1byte fputs("\r\n", fFile);//+2byte //total 15 (+ 4) byte //cursor += strlen(parent) + 4; return true; } bool Ini::Close(){ fclose(fFile); return true; } void Ini::Clear(){ fputs("\r\n", fFile);//to ignore the last trash read. caused by ??? look into }
Code://and the main file looks something like this: Ini *ini = new Ini(); ini->Open(szIniFile);//Open the file ini->Read(); if(ini->Select("ConnectionInfo")){ ini->ReadElement("IP"); ini->ReadElement("Port"); ini->ReadElement("User"); ini->ReadElement("Pass"); } if(ini->Select("LinkingInfo")){ ini->ReadElement("URLPath"); ini->ReadElement("Path"); } ini->Close();//Close the file
Please help me with solving this issue ASAP!
Regards, Oscar





Reply With Quote