   returning a char array
  Hello everybodybased on this tutorial I'm currently working on some functions to read/write ini-files. I'm trying to do it in C with no external libraries needed. The reading part works quite well. My problem however is, I don't know how to return the result (foundEntry = a char array).can anybody give me a hint?here's my ini file:[Section1][Section2]Entry1 = Value1Entry2 = Value2Entry3 = Value3[Section3]here's my code so far:#include <stdio.h>#include <string.h>#define INI_FILENAME "test.ini"#define MAX_LENGTH_OF_LINE 100 // maximum length of a line// the ini file should look something like this:// // [section1]// ; comment on section1// entry1 = valueofentry1// entry2 = valueofentry2//  // [section2]// ; comment on section 2// entry1 = valueofentry1// entry1 = valueofentry2// char *readFromINI(FILE *iniFile, char *section, char *entry){  printf("search section: %s\n", section);  printf("  search entry: %s\n", entry);  printf("--------------\n");  char buffer[MAX_LENGTH_OF_LINE];  // = holds the content of current line  char *foundSection;  // = picking out the section name  char *foundEntry;  // = picking out the entry name  int sectionLength = 0;  int entryLength = 0;  bool sectionFound = false;  //go through the lines until EOF is reached  while (fgets(buffer, MAX_LENGTH_OF_LINE, iniFile))  {  if (buffer[0]==';') {continue;}  if(sectionFound == false)  {    foundSection = buffer;    while (*foundSection && *foundSection!='[') {foundSection++;}    if (*foundSection!='[') {continue;}    *foundSection++ = 0; // delete "["    while (*foundSection && *foundSection!=']') {foundSection++; sectionLength++;}    if (*foundSection!=']') {continue;}    *foundSection++ = 0; // delete "]" while (*foundSection) {foundSection++;}  *--foundSection = 0;    for(int i = sectionLength+1; i > 0; i--){foundSection--;}    sectionLength = 0;   if (strcmp(foundSection, section) == 0) // compare the char arrays{ printf(" found section: %s\n", foundSection); sectionFound = true; continue;}  } // = end if sectionFound == false  if(sectionFound == true)  {  if (buffer[0]=='[') {sectionFound = false; continue;}    foundEntry = buffer;  while (*foundEntry && *foundEntry!='=') {foundEntry++;}   if (*foundEntry!='=') {continue;}    *foundEntry-- = 0; // delete " " before the "="    *foundEntry++ = 0; // delete " " before the "="    *foundEntry++ = 0; // delete "="    *foundEntry++ = 0; // delete " " after the "="    // Skip to end of string and delete lf (=line feed char)    while (*foundEntry) {foundEntry++; entryLength++;} *--foundEntry = 0;    // move back to the beginning of the value of the entry    for(int i = entryLength-1; i > 0; i--){foundEntry--;}    entryLength = 0;    if(strcmp(buffer, entry) == 0){printf("   found value: %s\n", foundEntry); return foundEntry;}  } // = end if sectionFound == true  } // = end of skipping through the lines}int main(void){  //open the file  FILE *cfgFile=fopen(INI_FILENAME, "r");  // if file doesn't exist. Quit right away  if (!cfgFile) {    fprintf(stderr, "Config file %s not found.\n", INI_FILENAME);    return 1;  }  //if file exists, read from it.  printf("\n");  printf("returned value: %s\n", readFromINI(cfgFile, "Section2", "Entry2"));  return 0;}currently, char *readFromINI(FILE *iniFile, char *section, char *entry) doesn't return anything. Strange enough, it does return something if I put something like return "test"; instead of return foundEntry;What am I doing wrong? Thanks!RomanHP.S. Once the whole thing works I will of course post the source code somewhere. I don't seem to be the only one looking for some ini-handling functions. :-)   
  Your function readFromINI() returns a char*, i.e. a pointer to a character buffer. The memory for this buffer must be provided somehow. A statement like 'return "test";' will let the compiler generate the text "test" somewhere in your executable, then return the address of this buffer with the return-statement. A way to provide the memory whose address will be returned by readFromINI() is either to call the malloc() function (note that the application is responsible to free the buffer when not in use anymore) or to let the application provide a buffer, i.e. 'char *readFromINI(FILE *iniFile, char *section, char *entry, char* outBuffer)'.   ;  OK, first of all, you're off to a great start. Keep it up!Second, I don't know the reason to stay with C, but I would highly recommend using the C++ standard template library. From vectors to strings to streams, it will help you immensly. If you were using an std::string right now, you would not be having this problem.Third, your problem is that you are declaring your buffer array within the local scope. Then, you are declaring a pointer to that buffer called foundEntry and then returning that pointer. When you go to use that pointer outside of the function, it will point at nothing because buffer, the memory that foundEntry originally pointed to, has gone out of scope and has been removed from the stack. IE - It no longer exists.There are a few ways around this. First, you could switch to using std::string, which will save you alot of hassle. Second, you could declare the pointer that you are returning on the heap, which means that it will still exist once the function ends and the pointers you are returning will still point to valid memory. To do this, do something like this:char *ret = new char[MAX_LENGTH_OF_LINE];strcpy( ret, foundEntry );return ret;Then, outside where you call your function, you must cleanup your pointer when you are done with it, otherwise you will get memory leaks:char *value = readFromINI( iniFile, section, entry );/* Do Stuff With Value */delete [] value;If you have other questions, be sure to ask!   ;  Thanks a lot! You guys are great! It works like a charm now!I'm trying to keep it in C because I know very little about Cpp. :-) I know, it might be a good moment to start getting into it...Actually, my knowledge of C is also still quite limited. That's also the reason for my "creative" usage of pointers. :-)I will now start with the writeToINI function, do some cleanup and post it here for everyone to use and (hopefully) enjoy.THANKS!!!RomanHbelow is the working source so far...#include <stdio.h>#include <stdlib.h>#include <string.h>#define INI_FILENAME "test.ini"#define MAX_LENGTH_OF_LINE 100 // maximum length of a line// the ini file should look something like this:// // [section1]// ; comment on section1// entry1 = valueofentry1// entry2 = valueofentry2//  // [section2]// ; comment on section 2// entry1 = valueofentry1// entry1 = valueofentry2// char *readFromINI(FILE *iniFile, char *section, char *entry){  printf("search section: %s\n", section);  printf("  search entry: %s\n", entry);  printf("--------------\n");  char buffer[MAX_LENGTH_OF_LINE];  // = holds the content of current line  char *foundSection;  // = picking out the section name  char *foundEntry;  // = picking out the section name  int sectionLength = 0;  int entryLength = 0;  bool sectionFound = false;  //go through the lines until EOF is reached  while (fgets(buffer, MAX_LENGTH_OF_LINE, iniFile))  {  if (buffer[0]==';') {continue;}  if(sectionFound == false)  {    foundSection = buffer;    while (*foundSection && *foundSection!='[') {foundSection++;}    if (*foundSection!='[') {continue;}    *foundSection++ = 0; // delete "["    while (*foundSection && *foundSection!=']') {foundSection++; sectionLength++;}    if (*foundSection!=']') {continue;}    *foundSection++ = 0; // delete "]" while (*foundSection) {foundSection++;}  *--foundSection = 0;    for(int i = sectionLength+1; i > 0; i--){foundSection--;}    sectionLength = 0;   if (strcmp(foundSection, section) == 0) // compare the char arrays{ printf(" found section: %s\n", foundSection); sectionFound = true; continue;}  } // = end if sectionFound == false  if(sectionFound == true)  {  if (buffer[0]=='[') {sectionFound = false; continue;}    foundEntry = buffer;  while (*foundEntry && *foundEntry!='=') {foundEntry++;}   if (*foundEntry!='=') {continue;}    *foundEntry-- = 0; // delete " " before the "="    *foundEntry++ = 0; // delete " " before the "="    *foundEntry++ = 0; // delete "="    *foundEntry++ = 0; // delete " " after the "="    // Skip to end of string and delete lf (=line feed char)    while (*foundEntry) {foundEntry++; entryLength++;} *--foundEntry = 0;    // move back to the beginning of the value of the entry    for(int i = entryLength-1; i > 0; i--){foundEntry--;}    entryLength = 0;    if(strcmp(buffer, entry) == 0){printf("   found value: %s\n", foundEntry);char *ret = new char[MAX_LENGTH_OF_LINE];    strcpy( ret, foundEntry );    return ret;    }  } // = end if sectionFound == true  } // = end of skipping through the lines}int main(void){  //open the file  FILE *cfgFile=fopen(INI_FILENAME, "r");  // if file doesn't exist. Quit right away  if (!cfgFile) {    fprintf(stderr, "Config file %s not found.\n", INI_FILENAME);    return 1;  }  char *value = readFromINI(cfgFile, "Section2", "Entry2");/* Do Stuff With Value */  //if file exists, read from it.  printf("\n");  printf("returned value: %s\n", value);  delete [] value;  return 0;};Quote:Original post by ussnewjersey4char *ret = new char[MAX_LENGTH_OF_LINE];strcpy( ret, foundEntry );return ret;Then, outside where you call your function, you must cleanup your pointer when you are done with it, otherwise you will get memory leaks:char *value = readFromINI( iniFile, section, entry );/* Do Stuff With Value */delete [] value;If you have other questions, be sure to ask!lol, that's very slick of you [grin]the actual C way is:char *ret = (int*)malloc(sizeof(char) * MAX_LENGTH_OF_LINE);strcpy( ret, foundEntry );return ret;Then, outside where you call your function, you must cleanup your pointer when you are done with it, otherwise you will get memory leaks:char *value = readFromINI( iniFile, section, entry );/* Do Stuff With Value */free(value);as far as I know, the (int*) is not needed; ie. optional.;Quote:lol, that's very slick of you the actual C way is:char *ret = (int*)malloc(sizeof(char) * MAX_LENGTH_OF_LINE);strcpy( ret, foundEntry );return ret;:) He was posting in for beginners, so I figured I would show him the easiest way. It just so happens that that way is also C++. Its good to work these things in now. He stated himself that he didn't really know either one really well, so it's not like he needs C because C++ will not work on his platform. So he should really take advantage of C++ features.   