Scope And Parameter Passing
essentially what im trying to do is to use one print-to-file function in two different ways. i have a print-to-error-file and a print-to-correct-file after i check some user input and i want to combine them.
i am having some problems when i pass in the parameters. here is what i pass:
1. BOOK *fptr as fptr, the pointer to the struct.
2. int num as goods or bads, a seperate counter to print to each seperate file (1. line one, 2. line two, etc.)
3. char *fil as good_file or bad_file for the file to print to
4. a as a for the 1st number of the multidimensional array to print
plz take a look and give me some ideas, thanks.
DECLARATIONS:
Code:
void printer(BOOK *fptr, int num, char *fil, int a);
FILE *good_file = NULL;
FILE *bad_file = NULL;
int s1 = 0, s2 = 0, s3 = 0; /* String length of read in lines */
CALL TO FUNCTION:
Code:
void automatic(BOOK *fptr)
{
int goods = 0, bads = 0, a = 0;
good_file = fopen("address book.txt", "w");
bad_file = fopen("errors.txt", "w");
...
...
if(good)
{ printer(fptr, goods, good_file, a); }
else
{ printer(fptr, bads, bad_file, a); }
}
FUNCTION:
Code:
void printer(BOOK *fptr, int num, char *fil, int a)
{
int l, m, n, o, p, q, r;
num++; /* Increment correct count for print out */
fprintf(fil, "%d. ", num); /* Print line number to file */
for(l = 0; l < UNIT_MAX_SIZE; l++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->flat_unit[a][l]); /* Print character to file */
} /* End for loop */
fprintf(fil, " "); /* Print space to file */
for(m = 0; m < NUMBER_MAX_SIZE; m++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->flat_number[a][m]); /* Print character to file */
} /* End for loop */
fprintf(fil, " %c ", fptr->flat_divider[a]); /* Print character to file */
for(n = 0; n < NUMBER_MAX_SIZE; n++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->number[a][n]); /* Print character to file */
} /* End for loop */
fprintf(fil, " "); /* Print space to file */
for(o = 0; o < s1; o++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->street_name[a][o]); /* Print character to file */
} /* End for loop */
fprintf(fil, " "); /* Print space to file */
for(p = 0; p < s2; p++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->street_type[a][p]); /* Print character to file */
} /* End for loop */
fprintf(fil, " "); /* Print space to file */
for(q = 0; q < s3; q++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->suburb_name[a][q]); /* Print character to file */
} /* End for loop */
fprintf(fil, " "); /* Print space to file */
for(r = 0; r < POSTCODE_MAX_SIZE; r++) /* For loop to print whole string to file */
{ /* Start for loop */
fprintf(fil, "%c", fptr->postcode[a][r]); /* Print character to file */
} /* End for loop */
fprintf(fil, "\n"); /* Print end of line to file */
}
COMPILE ERRORS:
(there are more above this but my ms-dos prompt scrolls through them to fast - but they are pretty much similar because of the same fprintf calls)
assign.c(406) : Error: need explicit cast for function parameter 3 to get
from: struct _iobuf*
to : char *
fprintf(fil, "%d. ", num);
^
assign.c(495) : Error: need explicit cast for function parameter 1 to get
from: char *
to : struct _iobuf*
fprintf(fil, "%c", fptr->flat_unit[a][l]);
^
assign.c(498) : Error: need explicit cast for function parameter 1 to get
from: char *
to : struct _iobuf*
fprintf(fil, " ");
^
assign.c(500) : Error: need explicit cast for function parameter 1 to get
Fatal error: too many errors
--- errorlevel 1
so overall this function is pretty screwed i know. but if someone has any ideas that would be great.
Got Another One For Passing Parameters
ok what im trying to do is to combine 3 bits of code that are exactly the same except for what the read in character is passed into.
firstly i read in from the file to a temp 'z', then do some checking which is all the same, and then i want to move it into the struct (but i want it to go to 3 different multidimensional arrays to store).
i cant figure out what to pass and how so to put the 1st in 'street_name', 2nd in 'street_type', & 3rd in 'suburb_name'.
check out my code and help if u can, thanx.
DECLARATION:
Code:
void readString(BOOK *fptr, char z, FILE *bfil, int a);
CALL TO FUNCTION:
Code:
readString(fptr, z, batch_in_file, a);
FUNCTION:
Code:
void readString(BOOK *fptr, char z, FILE *bfil, int a)
{
int b = 0; /* Declare temporary counting variables */
count = 0; /* Reset counter to read incoming string */
rester(); /* Call to reset word test variables */
for (b = 0; b < STRING_MAX_SIZE; b++) /* Run loop until maximum string size */
{ /* Start for loop */
if(ended == TRUE) /* Check if end of word has been reached */
{ /* Start if statement */
break; /* Break from for loop if at end of word */
} /* End if statement */
z = fgetc(bfil); /* Gather character from file to temporary variable */
if((z == ' ') && (started == FALSE)) /* Check if read in character is a space */
{ /* Start if statement */
b--; /* Reset counter back one to allow space */
} /* End if statement */
else /* Else read in character is not a space */
{ /* Start else statement */
started = TRUE; /* Set started to true if word has begun */
if((z == ' ') && (started == TRUE)) /* Check if character is a space and word has started */
{ /* Start if statement */
ended = TRUE; /* Set end to true if at end of word */
} /* End if statement */
else /* Else character is letter in word */
{ /* Start else statement */
count++; /* Add one to string size counter */
[B]fptr->street_name[a] = z; /* Move read in character into struct */
} /* End else statement */
} /* End else statement */
} /* End for loop */
}
what the heck do i put in the bolded bits so i can just call it and pass in where i want it to go. thanx again.