No, BUFSIZ is a user-defined constant. He wrote the code that way to tell you, the one taking the example, it's variable to whatever you want it to be.Quote:
Originally Posted by Hoser83
Printable View
No, BUFSIZ is a user-defined constant. He wrote the code that way to tell you, the one taking the example, it's variable to whatever you want it to be.Quote:
Originally Posted by Hoser83
so base 10 equals a decimal integer?
Yes. Do you understand the concept of bases, though? It's a basic computer concept that will be very essential in your progamming future.
Here is my entire code:
I'm not able to use my compiler right now so I'm just trying to get close:
My book has not gone over bases yet. That is why I'm sort of lost here.
If this does work, how do I use what is in the string to get the correct number of random numbers printed. Right now, I use a for loop, but I don't know for sure how to use the for loop with the string.
Code:#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
FILE *cfopen(char *filename, char *mode);
FILE *gfopen(char *filename, char *mode);
int main(void)
{
FILE *ofp;
int i, line[MAXLINE];
char filename[120];
char *pEnd;
long 1;
prinf("\n%S\n\n%s",
"This progam creates a file of random numbers.",
"Please enter in the number of random numbers
you would like to see as a positive integer. "),
fget( line, MAXLINE, stdin);
if((l = strtol(line, &pEnd, 10)) <= 0 || *pEnd != '\n')
{
printf("\nERROR: Please enter positive integer.\n");
}
printf("\nIn what file would you like them? ");
scanf("%s", filename);
ofp = cfopen(filename, "r+");
for(i=1; i<=line; ++i){
fprintf(ofp, "%12d", rand());
if(i%6 == 0 || i == n)
fprintf(ofp, "\n");
}
}
FILE *cfopen(char *filename, char *mode)
{
char reply[2];
FILE *fp;
if(strcmp(mode, "w") == 0
&& (fp = fopen(filename, "r")) != NULL){
fclose(fp);
printf("\nFile exists. Overwrite it? ");
scanf("%1s", reply);
if(*reply != 'y' && *reply != 'Y') {
printf("\nBye!\n\n");
exit(1);
}
fp = gfopen(filename, mode);
return fp;
}
FILE *gfopen(char *filename, char *mode)
{
FILE *fp;
if((fp = fopen(filename,mode)) == NULL){
fprintf(stderr, "Cannot open %s - bye!\n", filename);
exit(1);
}
return fp;
}