title: Return a value inside and outside the while loop in C
body: <p>I want to write a program which reads a file and breaks down its contents. The file basically contains a string containing IDs. The contents of the file are like this:</p>
  
  <pre><code>[root@ben-pc ~]# cat file
  1,2,3
  </code></pre>
  
  <p>I want to get the IDs 1,2,3 seperately returned from a parsing function. In my code i get the first two IDs within the while loop and the last ID outside the while loop. But I am not able to return all these values properly. I know I cannot use two <code>return</code> calls in the code like this. Can any one suggest how can I get this working as desired. Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;dirent.h&gt;
  
  char *get_group_members (char* file)
  {
      FILE *fp;
      char *str;
      char filename[64];
      int c;
      size_t n = 0;
  
      snprintf(filename, sizeof(char) * 64, "/home/ben/%s", file);
      fp = fopen(filename, "r");
  
      if(fp == NULL)
      {
              perror("Error opening file");
      }
  
      str = malloc(100);
  
      while ((c = fgetc(fp)) != EOF )
      {
              if(c != ',')
              {
                      str[n++] = (char) c;
              }
              else
              {
                      //return str; Not working
                      printf("%s,", str);
                      n = 0;
              }
      }
      //return str; Not working
      printf("%s", str);
      fclose(fp);
  }
  
  int main (void)
  {
  printf("Group Members are ::\n");
  printf("%s\n", get_group_members("file"));
  return 0;
  }
  </code></pre>