Thread: code help required

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    code help required

    I cant get this code to compile on jfe/gcc for windows. The compiler says "parse errror before the '=='" in the while(feof(iFile)) ==0) line, and "warning: assignment to `long int *' from `bool' lacks a cast" in the part where ive described below. It is probably some simple syntax error, but i currently lack the knowledge to identify it.

    here's the offending function:
    Code:
    int ProcessIn(const char *inFileName)
    {
    	FILE* iFile=NULL;
    	char lheaderchar;
    	char tmpLine[MAXLINELENGTH];
       Line2Type type=COM;
       int linecount=0;
       int compcount=0;
       long *hcomments = NULL;
       long *tmpcomments = NULL;
       
    	iFile = fopen(inFileName, "r");
    
    	/* open file for reading, read in each line 1by1, process lines via functions 1by1 */
    
    	while(feof(iFile)) == 0)
    	{
    		linecount++;  /* reinitialise line values*/
    		type=COM;
    			
    		if(fgets(tmpLine, MAXLINELENGTH, iFile) == tmpLine)
    		{	 
    			if ((fscanf(iFile, "%c", &lheaderchar) == 1) && (compcount < MAXCOMPONENTS))
    			{	   
    				if (lheaderchar == '#')     /* test for comment headers */
    				{
    					if ((tmpcomments = (long *)realloc(hcomments, (linecount * sizeof(long))) == NULL))   /* cast warning for this line!*/
    					{
    						fprintf(stderr,__FUNCTION__);
    						fprintf(stderr,"::Memory Allocation Error\n");
    					}
    											
    					hcomments = tmpcomments;					
    					hcomments[linecount] = ftell(iFile);  /* stores original file pos. of header comments*/
    				}					
    				else 
    				{
    				compcount++;
    					switch(lheaderchar)
    					{ 
    						case 'R' : case 'L' : case 'C' :
    							type = RLC;
    							break;
    						case 'D' : case 'V' : case 'I' :
    							type = DVI;
    							break;
    						default: break;
    					}
    				}
    				
    				
    				if (type >0) 
    				{
    					/*ProcessLine(tmpLine, lheaderchar, type); to be added*/	
    				}
    			}
    	
    		}
    	}
    }
    and the adt header file:
    Code:
    /* component.h - Component ADT */
    
    #ifndef COMPONENT_H
    #define COMPONENT_H
    
    typedef enum {COM, RLC, DVI} e_ComLineType;
    typedef enum e_ComLineType Line2Type;
    
    typedef struct {
    	char ctype;
    	char string_indentifier[4];
    	int node1;
    	int node2;
    	char val_strpid[22];
       char comment[28];
    } component;
    
    typedef component *ComPtr;
    
    
    int ProcessIn(const char *inFileName);
    
    void ProcessLine(char *inLine, char firstchar, int type);
    
    void ProcessOut(ComPtr inPtr);
    
    int ComPtrCompare(const void *cp1, const void *cp2);
    
    #endif

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Count how many opening parentheses are in the line that is giving the error. Now count how many closing parentheses there are.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while(feof(iFile)) ==0)
    Count the parenthesis.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    do'h!

    thanks

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    Post A second time...

    And now for some real questions, on pointer logic.

    Q1:
    In the main sub I declare & initialize a double pointer (*Comptr) to NULL, as well as a struct component array which will hold all the data read in from the files, formatted (the validation functions etc I havent made yet ).

    Then sorting through heaps of filname reading in code (which works fine) we get down to the assignment, *Comsptr = &comps[0]; which compiles fine in jfe/gcc for windows, but i dont get the debug message: printf("debug: now just before processin\n");, thereore it must be the assignment. The assingment makes sense to me, just not the compiler, what am I missing?


    Q2:
    Also, in order to assign the input data into the struct, I have made the functions: ProcessIn, ProcessLine which you can see below. Both of these have a ComPtr argument, which i pass the double pointer (named Comsptr) dereferenced from the main. correct me if im wrong: I will need a normal ComPtr to be declared in main, and resized using realloc in the ProcessIn function or ProcessLine function to make/resize an array of actual struct components? So, can I use pointers to resize an array of what its pointing to (components) in the other functions?

    I also need an array of double pointers to qsort my stuff later for output (i declared this already- *Comsptr as pointer to a pointer, not an array), and an array of pointers(typedef'd ComPtr) for actual assignment to the array of components, i think...(that's 3 arrays in total, if your confused - I am).

    So is my logic correct for this q'n? or would i be able to pass 1 non-resized non-arrayed pointer to the functions, and redefine for each new line of data, but then I need ** array for qsort. Where to start...

    *note the hcomments assigning part is erroneous, but irrelevant anyway, working on this now .*


    whew, any help would be greatly appreciated, a least in question 1.


    Here's the complete code, for your enjoyment:
    Code:
    /* Reads electrical circuit components from input file and outputs to another file, formatted*/
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "component.h"
    
    #define MAXFILENAMELENGTH 256
    #define MAXCOMPONENTS 1000
    #define MAXLINELENGTH 80
    
    int ProcessArgs(int argc, char *argv[], char **inFileNamePtr, char **outFileNamePtr);
    void PrintUsage(void);
    
    /* MAIN */
    /* Main subroutine*/
    int main(int argc, char *argv[])
    {
      int imode = 0;
      char usrResponse;
      char *inFileName = NULL;
      char *outFileName = NULL;
      FILE* inFile=NULL;
      FILE* outFile=NULL;
      ComPtr *Comsptr=NULL;
      component comps[5];
      
      /** Get the input and output file names **/
      /* 1. First try the command line arguments */
      if (argc > 1){
        ProcessArgs(argc, argv, &inFileName, &outFileName);
      }
      do {
        imode = 0;
        /* 2. Get any missing file names from the user */
        if (inFileName == NULL) {
          if ( (inFileName = (char *) malloc(sizeof(char)*MAXFILENAMELENGTH)) == NULL ) {
            fprintf(stderr,__FUNCTION__);
            fprintf(stderr,"::Memory Allocation Error\n");
            exit(EXIT_FAILURE);
          }
          printf("Enter the input file's name > ");
          while (scanf("%255s",inFileName) < 1 );
          while (getchar() != '\n');
          imode++;
        }
    
        if (outFileName == NULL) {
          if ( (outFileName = (char *) malloc(sizeof(char)*MAXFILENAMELENGTH)) == NULL ) {
            fprintf(stderr,__FUNCTION__);
            fprintf(stderr,"::Memory Allocation Error\n");
            exit(EXIT_FAILURE);
          }
          printf("Enter the output file's name > ");
          while (scanf("%255s",outFileName) < 1 );
          while (getchar() != '\n');
          imode++;
        }
        
        /* read in file, process and output results*/
         
        printf("debug: now just before ptr assign\n");
        *Comsptr = &comps[0];
        
        printf("debug: now just before processin\n");
        
        ProcessIn(inFileName, *Comsptr);     
        printf("debug: after processin\n");
        
        /*ProcessOut(outFileName, Comsptr); */
        printf("debug: after processout\n");
                
    	 /* DEBUG- after run through ops*/
        printf("%s\n%s\n", inFileName, outFileName);
    
        /*****************/
        /* Process files */
        /*****************/
      
        /* Clean up allocated memory */
        free(inFileName);
        inFileName = NULL;
    
        free(outFileName);
        outFileName = NULL;
    
    	 if (inFile != NULL){    
        	fclose(inFile);
        }
        if (outFile != NULL){ 
        	fclose(outFile);
        }
    
        if (imode > 0) {
          printf("Do you want to process another input file? (y/n) ");
          while ( scanf("%[^ynYN]",&usrResponse) == 1);
          while ( scanf("%[ynYN]",&usrResponse) < 1 );
          while ( getchar() != '\n' );
    
          imode = (toupper(usrResponse) == 'Y');
        }
      } while (imode > 0);
    
      return EXIT_SUCCESS;
    }
    
    int ProcessIn(const char *inFileName, ComPtr copr)
    {
    	FILE* iFile=NULL;
    	char lheaderchar;
    	char tmpLine[MAXLINELENGTH];
       Line2Type type=COM;
       unsigned int linecount=0;
       int compcount=0;
       long *hcomments = NULL;
       long *tmpcomments = NULL;
       
    	iFile = fopen(inFileName, "r");
    
    	/* open file for reading, read in each line 1by1, process lines via functions 1by1 */
    
    	while(feof(iFile)== 0)
    	{
    		linecount++;  /* reinitialise line values*/
    		type=COM;
    			
    		if(fgets(tmpLine, MAXLINELENGTH, iFile) == tmpLine)
    		{	 
    			if ((fscanf(iFile, "%c", &lheaderchar) == 1) && (compcount < MAXCOMPONENTS))
    			{	   
    				if (lheaderchar == '#')     /* test for comment headers */
    				{
    					if ((tmpcomments = (long *) realloc(hcomments, (sizeof(long)* linecount))) == NULL)
    					{
    						fprintf(stderr,__FUNCTION__);
    						fprintf(stderr,"::Memory Allocation Error\n");
    						exit(EXIT_FAILURE);
    					}
    											
    					hcomments = tmpcomments;					
    					hcomments[linecount] = ftell(iFile);  /*file pos. of h comments & add in comments later!*/
    				}					
    				else 
    				{
    				compcount++;
    					switch(lheaderchar)
    					{ 
    						case 'R' : case 'L' : case 'C' :
    							type = RLC;
    							break;
    						case 'D' : case 'I' : case 'V' :
    							type = DIV;
    							break;
    						default: break;
    					}
    				}
    				
    				if (type >0) 
    				{
    					ProcessLine(tmpLine, lheaderchar, type, compcount, copr);
    					/* and return comp struct vals???*/
    				}
    			}
    		}
    	}
    	return 0; /* return comp struct ptr for arrays?????*/
    }
    
    void ProcessLine(char *inLine, char firstchar, Line2Type type, int count, ComPtr ptr)
    { 	/* assign line parts to component value, array of pointers to adt*/
    	char tmpstring_indentifier[3];
    	int tmpnode1;
    	int tmpnode2;
    	char tmpval_strpid[22];
       char tmpcomment[28];
    	ComPtr InCom= NULL;
    	ComPtr tmpcom = NULL;
    	
    	InCom = ptr;
    	
    	if ((tmpcom = (ComPtr) realloc(InCom, (sizeof(ComPtr) * count))) == NULL) 
    	{
          fprintf(stderr,__FUNCTION__);
          fprintf(stderr,"::Memory Allocation Error\n");       
          exit(EXIT_FAILURE);
       }  											
       
    	InCom = tmpcom;
    		
    	InCom->ctype = firstchar;
    	
    	if (sscanf(inLine,"%*c %s %i %i %s, %28c", tmpstring_indentifier, &tmpnode1, &tmpnode2, tmpval_strpid, tmpcomment) >0);
    	{
    		strcpy(InCom->string_indentifier, tmpstring_indentifier);
    		InCom->node1 = tmpnode1;
    		InCom->node2 = tmpnode2;
    		strcpy(InCom->val_strpid, tmpval_strpid);
       	strcpy(InCom->comment, tmpcomment);
    	}
    
    	/*to be continued - now calls validate function for each component for each type*/
    	
    	
    }
    
    void ProcessOut(const char *outFileName, ComPtr inPtr)
    {
    	FILE* oFile=NULL;
    	
    	oFile = fopen(outFileName, "w");
    	
    	/*
    	
    	
    	printout??
    	qsort should be called in processin, not here?????
    	-sort by qsort & function ComPtrCompare:
    	then: qsort(ComPtr, count, sizeof(ComPtr), ComPtrCompare);
    		??? order results in: RCLDIV, ordered in group by value (RLC), or by aphlabetic model name (DIV)
    	 	
    	print to new file in order
    	
    	free(inPtr); and other mem  */	
    }
    
    int ComPtrCompare(const void *cp1, const void *cp2)
    { /* compares elements of components from qsort*/ 
    	int result = 0;
    	
    	/*const ComPtr elem1 = (*(component **) cp1);
    	const ComPtr elem2 = (*(component **) cp2);
    	
    	if (isParallel(cp1, cp2) !=0 )
    	{ 
    			compare alpha for DIV, value for RLC
    			
    			if 
    			result = strcmp( model name???)
    			
    		else
    		{	??
    		?
    		}
    	}
    	else
    	{
    		result = 1;  more?
    	}	*/
    		
    	return result;
    } 
    
    int isParallel(const void *sp1, const void *sp2)
    {
    	int isparallel = 0;		/*true =1*/
    	int check = 0;
    		
    	const ComPtr elem1 = (*(component **) sp1);
    	const ComPtr elem2 = (*(component **) sp2);
    	
    	check = elem1->node1 - elem2->node1;
    	if  (check == 0)
    	{
    		check = elem1->node2 - elem2->node2;
    		if (check == 0)
    		{
    			isparallel = 1;
    		}
    		else
    		{
    			isparallel = 0;
    		}
    	}
    	else if (check != 0)
    	{
    		check = elem1->node1 - elem2->node2;
    		if (check == 0)
    		{
    			check = elem1->node2 - elem2->node1;
    			if (check == 0)
    			{
    				isparallel = 1;
    			}
    		}
    		else
    		{
    			isparallel = 0;
    		}
    	}
    	return isparallel;
    }
    
    int
    ProcessArgs(int argc, char *argv[], char **inFileNamePtr, char **outFileNamePtr)
    {
      int argCount = 0;
    
      argc--;
      argv++;
      while (argc > 0) {
        if ( strcmp((*argv),"-i") == 0 ) {
          argc--;
          argv++;
          if (argc > 0) {
            if ( ( (*inFileNamePtr) = (char *) malloc( sizeof(char) * (strlen( (*argv) ) + 1)) ) == NULL ) {
              fprintf(stderr,__FUNCTION__);
    	  fprintf(stderr,"::Memory Allocation Error\n");
    	  exit(EXIT_FAILURE);
            }
            strcpy((*inFileNamePtr), (*argv));
            argCount++;
            argc--;
            argv++;
          }
        }
        else if ( strcmp((*argv),"-o") == 0 ) {
          argc--;
          argv++;
          if (argc > 0) {
            if ( ((*outFileNamePtr) = (char *) malloc(sizeof(char) * (strlen(*argv)+1)) ) == NULL ) {
              fprintf(stderr,__FUNCTION__);
    	  fprintf(stderr,"::Memory Allocation Error\n");
    	  exit(EXIT_FAILURE);
            }
            strcpy((*outFileNamePtr), (*argv));
            argCount++;
            argc--;
            argv++;
          }
        }
        else if ( strcmp((*argv),"--help") == 0) {
          PrintUsage();
          exit(EXIT_FAILURE);
        }
        else {
          fprintf(stderr,"Unknown Argument::%s\n",(*argv));
          PrintUsage();
          exit(EXIT_FAILURE);
        }
      }
    
      return argCount;
    }
    
    void
    PrintUsage(void)
    {
      fprintf(stderr,"usage:  fileProcessor -i s -o s\n\n");
      fprintf(stderr,"  -i       's' is the input file's name\n");
      fprintf(stderr,"  -o       's' is the output file's name\n");
      fprintf(stderr,"  --help   prints this usage message\n\n");
    }
    and the .h file from before, slightly edited:

    Code:
    /* component.h - Component ADT */
    
    #ifndef COMPONENT_H
    #define COMPONENT_H
    
    typedef enum {COM, RLC, DIV} Line2Type;
    
    typedef struct {
    	char ctype;
    	char string_indentifier[3];
    	int node1;
    	int node2;
    	char val_strpid[22];
       char comment[28];
    } component;
    
    typedef component *ComPtr;
    
    int ProcessIn(const char *inFileName, ComPtr copr);
    
    void ProcessLine(char *inLine, char firstchar, Line2Type type, int count, ComPtr ptr);
    
    void ProcessOut(const char *outFileName, ComPtr inPtr);
    
    int ComPtrCompare(const void *cp1, const void *cp2);
    
    int isParallel(const void *sp1, const void *sp2);
    
    #endif

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    *Comsptr = &comps[0];
    You don't want that asterick. With it in, you're assigning the address of the first element of comps to whatever Compsptr is pointing to (ie, NULL). This is a seg fault. With it out, you're assigning the address of comps[0] to Compsptr; to make Compsptr point to comps[0].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    See the FAQ on pointers.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ugh,
    Code:
    ComPtr *Comsptr=NULL;
    is like this:
    Code:
    component **Comsptr = NULL;
    I assume you meant
    Code:
    ComPtr Comsptr = NULL;
    or
    Code:
    component *Comsptr = NULL;
    The typedef adds an asterick, don't add another one yourself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Now you know why people hate typecast pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    thank you for the answers; makes sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code required for the problem..
    By varun_mukthi in forum C Programming
    Replies: 4
    Last Post: 01-08-2007, 01:27 PM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM