The following creates and prints a table of random numbers onto the console. How can I modify the createtxt function I made, so that the output on the console is generated into a text file at the same time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
FILE* createtxt(char* fnam){
FILE* ofp;
ofp = fopen(fnam, "w");
if (ofp == NULL) {
printf("Cannot open output file %s\n", fnam);
exit(EXIT_FAILURE);
}
void closetxt(FILE* ofp){
fclose(ofp);
}
int main (void){
printf("Table of random numbers for drawing geometric shapes in different colours, sizes and opacities\n");
int rn = 0;
unsigned int seed = (unsigned int)time(NULL);
srand(seed);
int k = 0;
printf("shape#\tRSCE\tx\ty\twidth\theight\tred\tgreen\tblue\tgreen\topacity\n");
while (k < NO_SHAPES){
printf("%6d", k);
rn = rand() % SHAPE_RANGE;
printf( "\t%4d",rn);
rn = rand() % X_RANGE;
printf("\t%d",rn);
rn = rand() % Y_RANGE;
printf("\t%d",rn);
rn = rand() % WIDTH_RANGE;
printf("\t%5d",rn);
rn = rand() % HEIGHT_RANGE;
printf("\t%6d",rn);
rn = rand() % RED_RANGE;
printf("\t%3d",rn);
rn = rand() % GREEN_RANGE;
printf("\t%5d",rn);
rn = rand() % BLUE_RANGE;
printf("\t%4d",rn);
rn = rand() % OPACITY_RANGE;
printf("\t%.1f\n",rn/100.0);
k++;
}
FILE* ofp = createtxt("myrandom.txt")
closetxt(ofp);
return EXIT_SUCCESS;
}
I'd probably think about creating an
ffprintf()
function:Then the main code can be:
If you don't feel comfortable using variable length argument lists, you can write a simpler version of the function:
Now you have to write the header twice:
Most of the rest can stay unchanged, but the opacity (a float) needs to be handled specially as well as the header line.
Clearly, the function name is changeable if you don't like that it encroaches on the standard names for function. If you go with the simpler approach, the function name should be changed to something like
ffprint_int()
since it only prints oneint
at a time.The use of
assert()
is lazy, but it does ensure that both print operations succeeded (or both failed). If you're worried about it, change that assertion into something like:This will return the smaller of the two values; if one is -1 (complete failure), that's the value that will be returned. Of course, since the code I wrote ignores the return value from
ffprintf()
, just as your code ignored the return value fromprintf()
, this is a nuance that will go largely unnoticed.This version is Done in-line with the printf() statements: (see below for in
createtxt()
open the file before the printf statements:
If from within createtxt:
Change the prototype to
int createtxt(char *fnam, char *buf)
rather than using
Use:
//create and initialize a bigger buffer.
Then pass buf as argument to
createtxt()
.