Useful Functions


Several useful functions you will want to be aware of from the c library include:

  • size_t strlen (const char *s) calculates the size of null-terminated strings.

  • int strcmp (const char *sl, const char *s2) compares two strings alphabetically.

  • char * strdup (const char *s) takes the pointer to a string, and creates a new copy in a new location, and returns the new location.

  • FILE * fopen (const char *filename, const char *opentype) opens a managed, buffered file (allows easier reading and writing than using file descriptors directly).[3], [4]

  • int fclose (FILE * stream) closes a file opened with fopen.

  • char * fgets (char *s, int count, FILE *stream) fetches a line of characters into string s.

  • int fputs (const char *s, FILE *stream) writes a string to the given open file.

  • int fprintf (FILE *stream, const char *template, ...) is just like printf, but it uses an open file rather than defaulting to using standard output.

You can find the complete manual on this library by going to http://www.gnu.org/software/libc/manual/

[3]stdin, stdout, and stderr (all lower case) can be used in these programs to refer to the files of their corresponding file descriptors.

[4]FILE is a struct. You don't need to know its contents to use it. You only have to store the pointer and pass it to the relevant other functions.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net