Yeah, I got it to work that way. Thanks. Hey Richie if you have time to look at this one last thing. It's the same program with some modifications I made so that if you input something like 46i instead of 461, it causes an error. I can get it to tell me there is an error and to write to a file, but I don't think it gives me the right number of random numbers. Can you run it and see what you come up with. Thanks man. Am I printing them out right in my for loop?
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;
char filename[120],*pEnd ,line[MAXLINE];
printf("\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. "),
fgets( line, MAXLINE, stdin);
if((strtol(line, &pEnd, 0)) <= 0 || *pEnd != '\n')
{
printf("\nERROR: Please enter positive integer.\n");
}
printf("\nIn what file would you like them? ");
scanf("%s", filename);
ofp = cfopen(filename, "w");
for(i=1; i<=(int)line; ++i){
fprintf(ofp, "%12d", rand());
if(i%6 == 0 || i == (int)line)
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;
}