Thread: functions & external files

  1. #1
    Unregistered
    Guest

    functions & external files

    I have the following code and there are two sections commented out that i want to make into functions arg_val and print_data these functions will go into a separate file called func.c im not to sure about making these functions that will work in a external file

    anyway heres the code ||||||
    VVVV

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define MAXLEN 81


    struct used
    {
    int ln;
    struct used *next;
    };

    struct identifier
    {
    char ID [11];
    int definition;
    int ismultdef;
    struct identifier *next;
    struct used *use;
    };

    int main (int argc, char* argv[])
    {
    struct identifier *head=NULL;
    struct identifier *cur=NULL;
    struct used *cur2=NULL;
    char *idname;
    char line [MAXLEN];
    FILE *foutp, *finp;
    int linenum=1;
    int COUNT=0;


    **************************************************
    *************************************************
    i want to take the following code and make it a function called arg_val
    and this will be put into a separate file called func.c
    ************************************************** *

    if (argc < 3)
    {
    printf("usage : p2 input output -m -n \n");
    exit (1);
    }
    if (argc >5)
    {
    printf("usage : p2 input output -m -u \n");
    exit (1);
    }
    if((strcmp(argv[1],argv[2])==0))
    {
    printf("test\n");
    exit (1);
    }



    if(argc==4)
    {
    printf("ok\n");
    if(strcmp(argv[3], "-m")==0)
    printf("-m\n");
    else if (strcmp(argv[3], "-u")==0)
    printf("-u\n");
    else
    {
    printf("error invalid flags usage -m -u\n");
    exit(1);
    }
    }

    if(argc==5)
    {
    printf("ok\n");

    if(strcmp(argv[3], "-m")==0)
    printf("-m\n");
    else if (strcmp(argv[3], "-u")==0)
    printf("-u\n");
    else
    {
    printf("error invalid flags usage -m -u\n");
    exit(1);
    }
    if(strcmp(argv[3],argv[4])==0)
    printf("test1\n");
    else
    {
    if(strcmp(argv[4],"-m")==0)
    printf("-m\n");
    else if (strcmp(argv[4], "-u")==0)
    printf("-u\n");
    else
    {
    printf("error\n");
    exit(1);
    }
    }
    }

    if((finp=fopen(argv[1],"r"))==NULL)
    {
    printf("error\n");
    exit(1);
    }

    if((foutp=fopen(argv[2],"w"))==NULL)
    {
    printf("error\n");
    exit(1);
    */*************************************************
    *************end of arg_val**************************
    }


    while(fgets(line, MAXLEN,finp)!=NULL)
    {
    int COUNT=0;
    if(line[COUNT]=='#')
    {
    fprintf(foutp,"%d %s", linenum++,line);
    continue;
    }

    if((line[COUNT]=='_') || isalpha(line[COUNT]))
    {
    fprintf(foutp,"%d %s", linenum++,line);
    idname=strtok(line,":");
    if(head==NULL)
    {

    head=(struct identifier *)malloc(sizeof(struct identifier));
    head->definition=linenum-1;
    head->ismultdef-1;
    head->next=NULL;
    head->use=NULL;
    strcpy(head->ID, idname);
    }

    else
    {
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    if(strcmp(cur->ID, idname)==0)
    {
    if(cur->ismultdef==0)
    cur->definition=linenum-1;
    cur->ismultdef++;
    break;
    }
    if(cur->next==NULL)
    {
    cur->next=(struct identifier *)malloc(sizeof(struct identifier));
    cur=cur->next;
    cur->definition=linenum-1;
    cur->ismultdef=1;
    cur->next=NULL;
    cur->use=NULL;
    strcpy(cur->ID, idname);
    break;
    }
    }
    }
    idname=strtok(NULL,"\t\n,");
    while(idname!=NULL)
    {
    idname=strtok(NULL,"\t\n,");
    if(idname==NULL)
    break;
    if(isdigit(idname[0])||idname[0]=='\"'||idname[0]=='\''||idname[0]=='#')
    break;
    if(isalpha(idname[0])||idname[0]=='_')
    {
    if(head==NULL)
    {
    head=(struct identifier *)malloc(sizeof(struct identifier));
    head->ismultdef=0;
    head->next=NULL;
    strcpy(head->ID,idname);
    head->use=(struct used *)malloc(sizeof(struct used));
    cur2=head->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    }
    else
    {
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    if(strcmp(cur->ID,idname)==0)
    {
    if(NULL==cur->use)
    {
    cur->use=(struct used *)malloc(sizeof(struct used));
    cur2=cur->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    }
    else
    {
    for(cur2=cur->use;cur2!=NULL;cur2=cur2->next)
    {
    if(cur2->ln==linenum-1)
    break;
    if(cur2->next==NULL)
    {
    cur2->next=(struct used*)malloc(sizeof(struct used));
    cur2=cur2->next;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    break;
    }
    }
    }
    break;
    }
    if(cur->next==NULL)
    {
    cur->next=(struct identifier *)malloc(sizeof(struct identifier));
    cur=cur->next;
    cur->ismultdef=0;
    cur->definition=-1;
    cur->next=NULL;
    strcpy(cur->ID,idname);
    cur->use=(struct used *)malloc(sizeof(struct used));
    cur2=cur->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    break;
    }
    }
    }
    }
    }
    continue;
    }
    if((line[COUNT]==' ')|| (line[COUNT]=='\t'))
    {
    while((line[COUNT] ==' ')||(line[COUNT]=='\t'))
    {
    COUNT++;
    }

    if(line[COUNT]=='#')
    {
    fprintf(foutp,"%d %s",linenum++,line);
    continue;
    }

    if((line[COUNT]=='.')||(isalpha(line[COUNT])))
    {
    fprintf(foutp,"%d %s",linenum++,line);
    idname=strtok(line,"\t\n,");
    while(idname!=NULL)
    {
    idname=strtok(NULL,"\t\n,");
    if(idname==NULL)
    break;
    if(isdigit(idname[0])||idname[0]=='\"'||idname[0]=='\''||idname[0]=='#')
    break;
    if(isalpha(idname[0])||idname[0]=='_')
    {
    if(head==NULL)
    {
    head=(struct identifier *)malloc(sizeof(struct identifier));
    head->ismultdef=0;
    head->next=NULL;
    strcpy(head->ID,idname);
    head->use=(struct used *)malloc(sizeof(struct used));
    cur2=head->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    }
    else
    {
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    if(strcmp(cur->ID,idname)==0)
    {
    if(NULL==cur->use)
    {
    cur->use=(struct used *)malloc(sizeof(struct used));
    cur2=cur->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    }
    else
    {
    for(cur2=cur->use;cur2!=NULL;cur2=cur2->next)
    {
    if(cur2->ln==linenum-1)
    break;
    if(cur2->next==NULL)
    {
    cur2->next=(struct used *)malloc(sizeof(struct used));
    cur2=cur2->next;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    break;
    }
    }
    }
    break;
    }
    if(cur->next==NULL)
    {
    cur->next=(struct identifier *)malloc(sizeof(struct identifier));
    cur=cur->next;
    cur->ismultdef=0;
    cur->definition=-1;
    cur->next=NULL;
    strcpy(cur->ID,idname);
    cur->use=(struct used *)malloc(sizeof(struct used));
    cur2=cur->use;
    cur2->ln=linenum-1;
    cur2->next=NULL;
    break;
    }
    }
    }
    }
    }
    continue;
    }
    ************************************************** *
    ****this also should go into func.c as a function print_dat..........*
    ************************************************** *
    }
    if(line[COUNT]=='\n')
    fprintf(foutp,"\n");
    }
    fprintf(foutp,"\n\n\t cross reference table\n");
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    fprintf(foutp,"%s\t\t%d\t\t", cur->ID,cur->definition);
    for(cur2=cur->use;cur2!=NULL;cur2=cur2->next)
    fprintf(foutp,"%d ",cur2->ln);
    fprintf(foutp,"\n");
    }
    fprintf(foutp,"\nmultiply defined \n");
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    if(cur->ismultdef>1)
    fprintf(foutp,"%s\n",cur->ID);
    }
    fprintf(foutp,"\nundefined\n");
    for(cur=head;cur!=NULL;cur=cur->next)
    {
    if(cur->definition==-1)
    fprintf(foutp,"%s\n",cur->ID);
    }
    */**************************************************
    **********end of print_dat..........*************************
    *************************************************
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Test your functions to make sure they work correctly.
    Next, once they're working correctly, stick them in their own .c file.
    In you .c file, include the needed #include stuff, and also include something like this:

    #include "func.h"

    Your 'main()' function file will also use this line.

    Now you create a header file that will contain the prototypes for the functions included in func.c

    Then you should be able to compile your items correctly.

    If you need further help, and you need to post code like you've done, please use the [ c o d e ] [ / c o d e ] tags around your code (without all the spaces in there). This will preserve proper indentation and make your code more readable.

    Quzah.

  3. #3
    Unregistered
    Guest
    thats the problem im not sure what my function prototyps should look like also im not sure what to put in the include stuff

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    int main (int argc, char* argv[]);

    is the prototype of main().

    int main (int , char*);

    is also valid.

    Prototypes are just like variable declarations, except they are for functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. separating functions into diff files
    By cuizy in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 03:50 PM
  2. Replies: 7
    Last Post: 02-01-2009, 04:53 PM
  3. Input/Output to external files, I NEED HELP!!
    By MrMax91423 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2006, 08:45 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Functions and Creating Files
    By AngelEyes in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2002, 06:12 PM