#include <lcstring.h> char *strsave(const char *str);
strsave
allocates a private copy of a character string (using the
malloc
function).
strsave
returns the address of the copy of the string, or NULL
if no memory is available for a copy.
The copy should be released by a call to free
when it is no longer
required.
#include <lcstring.h> #include <stdio.h> static char *filename; static int couldopen(void); main() { if (couldopen() == 0) printf("The file "%s" was opened successfully.n", filename); else printf("The file "%s" was not opened successfully.n", filename); return; } int couldopen(void) { char buf[FILENAME_MAX]; FILE *f; puts("Enter a file name:"); gets(buf); filename = strsave(buf); /* Save the file name. Because */ /* buf is auto, saving the */ /* address of buf is not safe. */ f = fopen(buf, "r"); if (f){ fclose(f); return 0; /* success */ } else return -1; /* failure */ }
strcpy
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.