Hi guys,
A MFC coder want to learn some basic about charecter array intialisation and deletion of element.Take following examples campare with MFC (there is CString so no need of memory allocation or de allocation but same needed in c.)
(dont have std::string interface :-()
Example 1:-
To construct string we us following code in MFC.
CString constructString;
constructString = "";
constructString = "ABC";
constructString = constructString + "PQR";
constructString = constructString + "LMN";
whatever size of string we have this will work.
for C i used following code
#define DEFAULT_ARRAY_SIZE 20000
char* constructString = new char[DEFAULT_ARRAY_SIZE];
strcpy(constructString ,"");
strcat(constructString ,"ABC");
strcat(constructString ,"PQR");
strcat(constructString ,"LMN");
Problem :-
1)Code will work fine till my char* constructString size is less than 20000 but when it exceed i dont have solution,how to resize my array so it will take more charecters.
2)I intialize char* constructString with 20000 but when my string is very small of size 10 then my remaining 18990 charecters are wasted or not i dont know,will this effect my executable perfomance.If yes then how to delete my remaining dummy charecters.
Example 2:-
To read content from file we use following code in MFC.
CStdioFile ReadFile;
ReadFile.Open("Sample.txt",CFile::typeText|CFile::Read);
CString CurrentString;
CStringArray WholeFile;
while(ReadFile.ReadString(CurrentString))
{
WholeFile.Add(CurrentString);
}
Whitever size of File it will work fine.
For C i use following code
#define MAX_FILE_SIZE 65534
FILE *ptr_file;
const char* list[MAX_FILE_SIZE];
wchar_t CurrentString[1000];
ptr_file =fopen("Sample.txt","rb");
int __index = 0;
while(fgetws (CurrentString , 1000 , ptr_file) != NULL)
{
char* errorDes;
errorDes = new char[1000];
wcstombs(errorDes, CurrentString, 1000);
list[__index] = errorDes;
__index++;
}
Problem :-
1)Same as above if my one line charecters exceed 1000 then more than 1000 charecters are not consider and vise versa.
2)If my file size exceed 65534 then char* list array will not fill properly and vise versa.
________________________________________
Please provide me any link,block of code,suggestion that help me to solve all problem in pure c.
Thanks.