xboxscene.org forums

Pages: 1 [2]

Author Topic: Quick Array Question  (Read 246 times)

JapanFred

  • Archived User
  • Full Member
  • *
  • Posts: 116
Quick Array Question
« Reply #15 on: January 20, 2005, 10:38:00 AM »

Does is return the file AND them characters? Or just them characters?
Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #16 on: January 20, 2005, 10:58:00 AM »

within the function it returns the characters out of the file. the return from the function is displaying only those characters. The first line conf[0] has the weird characters, and the second line conf[1] seems to be blank.
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Quick Array Question
« Reply #17 on: January 20, 2005, 11:32:00 AM »

CODE

char* getConfig()
{
   debug("Opening Config file");
   FILE *fp;
   if((fp = fopen("D:\\xblock.xml","r")) != NULL)
   {
  char *conf[2] = {NULL};
  char line[100];
  int i = 0;
  //loop through lines
  while (!feof(fp)){
     fgets(line, 50, fp);
     debug(line);
     conf = line;
     i++;
  }
  fclose(fp);
  return *conf;
   }
   else
   {
  return "";
   }
}


Okay.  Basically, there are two different parts of memory from which you can allocate space for variables. when you do char line[100], it creates the space on the stack.  This is good for temporary variables because the compiler takes care of allocation and deallocation.  To be exact, that memory is considered defined at the start of that function, and undefined at the end of that function.  Since you want to pass chunks of memory around between functions you need to allocate space on the heap for the strings you want to copy around.  However, there's nothing wrong with first copying the line to a temp stack buffer.  Also, in C, when you use [] you are basically dereferencing into an array right? (like, give me the nth item in the array).  Well as we learnt above in this thread, a string is nothing more than an array of characters.  My point is that you don't actually pass back a char *, but a char ** because you are sending back a pointer to a list of pointers of character arrays.  You should read the C programming thing on howstuffworks.com about pointers and it should make it clearer.  

That pointer thing is also why you were returning *conf when I'd bet you originally had conf and the compiler complained smile.gif

anyways, You should do something in there more like this:

CODE

char** getConfig()
{
   debug("Opening Config file");
   FILE *fp;
   if((fp = fopen("D:\\xblock.xml","r")) != NULL)
   {
  char *conf[2] = {NULL};
  char line[100];
  int i = 0;
  //loop through lines
  while (!feof(fp)){
     fgets(line, 50, fp);
     debug(line);
        // allocate space on the heap for the string
        conf = (char *) malloc(sizeof(char) * (strlen(line) + 1));
        // copy the string from the temp buffer to our returned value
     strcpy(conf, line);
     i++;
  }
  fclose(fp);
  return conf;
   }
   else
   {
  return 0x00;  // you should use nulls when dealing with pointers in general to deal with errors.
   }
}



This was written on the fly and I just woke up but it should work I think.
Logged

Butcher_

  • Archived User
  • Full Member
  • *
  • Posts: 181
Quick Array Question
« Reply #18 on: January 23, 2005, 09:51:00 AM »

CODE

char** getConfig()
{
  debug("Opening Config file");
  FILE *fp;
  if((fp = fopen("D:\\xblock.xml","r")) != NULL)
  {
    char **conf;
    char line[100];

    conf = malloc(2 * sizeof(char*));
    memset(conf, 0, 2 * sizeof(char*));
    
    //loop through lines
    for (int i = 0; !feof(fp) && i < 2; ++i)
    {
      fgets(line, 50, fp);
      debug(line);
      // allocate space on the heap for the string and copy it in
      conf = strdup(line);
    }
    fclose(fp);
    return conf;
  }
  else
  {
    return NULL;  // you should use nulls when dealing with pointers in general to deal with errors.
  }
}


Don't forget to free each element of conf and conf itself afterwards:
CODE

for (int i = 0; i < 2; ++i)
  free(conf);
free(conf);
Logged
Pages: 1 [2]