Guides:C/C Crash Course/Writing and Using Function Libraries

From CoderGuide

Jump to: navigation, search

Back to TOC

Writing and Using Function Libraries

Okay, now that we have our header file that we typed in above, lets write our function code (you'll want to call this file safeio.c):

safeio.h was created in Compiler Directives and Header Files

#include "safeio.h"
**  sgets()   **
char *sgets(char *s, int size){
        int i;
        char ch;
        /*We have i=size-1, because we need space to add the
          null terminator. */
        for(i=0;ch!='\n' &&  i<size;i++){
                /*make sure we haven't reached the end of
                  the input stream*/
        s[i]=0; /*add the null terminator*/
        if(ch!='\n' && !feof(stdin)){
                for(;ch!='\n' && !feof(stdin); ch=getchar());
        return s;
} /** end of sgets() **/
**  sgeti()   **
long sgeti(){
        char s[80];
        /*read in a string, convert it to a long integer*/
        return atol(sgets(s,80));
}/** end sgeti()**/
**  sgetf()   **
double sgetf(){
        char s[80];
        /*read in a string, convert it to a double*/
        return atof(sgets(s,80));
}/** end sgetsd()**/
** sprompts() **
char *sprompts(char *prompt, char *s, int size){
        return sgets(s,size);
}/** end sprompts() **/
**  sgetc()   **
char sgetc(){
        char ch,c;
        /*Uh oh, no more data in the input stream*/
        if(feof(stdin))return 0;
        /*Keep reading characters until we reach the end of the
        line, or the end of the buffer.*/
        for(;c!='\n' && !feof(stdin);c=getchar());
        return ch; /*Return the first read character*/
}/*end sgetc()*/
** spromptc() **
char spromptc(char *prompt){
        return sgetc();
}/** end sprompts() **/

Whew! all done. Now that we have our library all written, lets write ourselves a program to test it. Call this file safe-test.c:

  safeio-test.c -- a program to test out our safe I/O library.
#include <stdio.h>
#include "safeio.h"
/*Why not see the #define directive in action :-)*/
#define STRSIZE 80 
int main(){
        char s[STRSIZE];
        long i;
        double d;
        printf("\tI got \"%s\"\n",s);
        printf("\tI got %c\n",s[0]);
        printf("\tI got %ld\n",i);
        printf("\tI got %f\n",d);
        printf("\tI got \"%s\"\n",s);
        printf("\tI got %c\n",s[0]);

Now it's time to compile this code. If you copied and pasted the code into your text editor, then there should be no errors at all. How you compile your code depends entirely on the compiler you're using. In Borland Builder and Microsoft Visual C++, you have to go through several steps in creating a project, adding files, etc to use a library. Using command line development tools, like GCC and MinGW, it's a lot simpler. he instructions here are exactly the same for Mac OS X Xcode, MinGW, Linux, BSD (using GCC), and all other systems using GNU tools.

The first thing you want to do is compile safeio.c, but not link it. Instead, you want the compiler to generate an object file. GCC gives object files the .o extension (Borland, Microsoft uses the .obj extension). To do this, you need to use the "-c" flag when compiling and specify the output file as safeio.o.

Caution: Make sure you do not use the ".c" extension for your output file, otherwise you will overwrite your source code file!!

The next thing that you want to do is compile your test program, and link it to your new safeio.o object file. So here is everything that you type the following in your terminal or MSYS window (gcc compilers only):

gcc -c safeio.c -o safeio.o
gcc safeio-test.c safeio.o -o safeio

Now you're all set to run your test program! On Unix systems, you'll probably need to specify the current directory when running a program (unless you've added "." to your path). So type ./safeio to run your program.

If you're using MinGW, you will want to use the MSYS shell to compile your program, but you don't want to run your program in the MSYS shell because of a bug in the way it handles console IO. Instead, you want to open a command prompt by running either cmd.exe (Windows NT/XP) or (All Microsoft operating systems), or you can run the program from explorer, by locating it, and clicking on it.

Personal tools