> > Here is my script, what I do is read a text file (Host file for
> > servers) in a function. The data is in the array "buff" (that works
> > fine), but when the function ends I get back in main() where I want to
> > use the gathered data in array "buff". I understand that I can't
> > return the data in "buff" to main() and that I have to use a pointer
> > from main() to get to the data. But I really have no clue how to
> > implement this into my script. Please help.
> > /* Declare variables*/
> > /* ========================================================================
> > */
> > char HOSTFILE[] = "d:\\scripts\\lcc\\novellping\\novellhosts.txt";
> > enum {MAX_LEN = 10240};
> > /* Pull in all required C header files */
> > /* ========================================================================
> > */
> > #include <stdio.h>
> > /* Declare function definitions */
> > /* ========================================================================
> > */
> > void read_hostfile(FILE *fin);
> > /* =======================================================================
> > */
> > /* MAIN */
> > /* =======================================================================
> > */
> > int main() {
> > FILE *fptr;
> > if ((fptr = fopen(HOSTFILE, "r")) == NULL) {
> > printf ("File %s cannot be opened for reading.\n", HOSTFILE);
> > } else {
> > printf ("Function \"read_hostfile()\" is now executed.\n");
> > read_hostfile(fptr);
> > printf ("\nBack to main().\n\n");
> > }
> > printf ("This is where I want to access the data in the array
> > \"buff\" from \"main()\".\n");
> > printf ("\n\nEnd of main() program.\n");
> > return 0;
> > }
> > /* ========================================================================
> > */
> > /* Function definitions */
> > /* ========================================================================
> > */
> > /* Read all data from the HOSTFILE and put it in the array "buff" */
> > void read_hostfile(FILE *fin) {
> > int num;
> > char buff[MAX_LEN + 1];
> > while (!feof(fin)) {
> > num = fread(buff, sizeof(char), MAX_LEN, fin);
> > }
> > printf ("\n\n%s\n\n", buff);
> > printf ("The enumerated size of array \"buff\" is: %i\n",
> > sizeof(buff));
> > printf ("The real size of array \"buff\" is: %i\n", num *
> > sizeof(char));
> > printf ("\n\nFile %s read and put in array \"buff\".\n", HOSTFILE);
> > }
> There are two possibilities: Either define 'buff' in main() and pass
> the buffer to read_host() or define 'buff' in read_host() as static
> and return the address of the buffer to main. I.e. either
> void read_host( FILE *fin, char *buff );
> int main( void )
> {
> char buff[ MAX_LEN + 1 ];
> ....
> read_host( fptr, buff );
> ....
> }
> void read_host( FILE *fin, char *buff )
> {
> ....
> }
> or
> char *read_host( FILE *fin );
> int main( void )
> {
> char *buff_from_read_host;
> ...
> buff_from_read_host = read_host( fptr );
> ...
> }
> char *read_host( FILE *fin )
> {
> char buff[ MAX_LEN + 1 ];
> ....
> return buff;
> }
> In the second case you have to be aware that calling read_host() a second
> time will overwrite what has been written into buff during the first call,
> so if main() still needs the information from the first invocation it has
> to copy it into some additional buffer before calling read_host() again.
> Regards, Jens
Thankx for posting this solution Jens.