Hey guys. I need some help..bad. I have to write a program that creates a file of randomly distributed numbers. I have the functions figured out and some of the main(). Could you guys take a look at it and just try to steer me in the right direction. I'm just confused on how to get a filename from user, and how many numbers they want to see, then acutally do it. I just need some help getting started.
Heres the code:
Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void get_info(char *filename, int *n_ptr);
FILE *cfopen(char *filename, char *mode);
FILE *gfopen(char *filename, char *mode);
int main( int argc, char **argv)
{
FILE *ofp;
int i;
// I don't know if I've declared all that I should, and I'm not
// sure how to call the functions.
for(i=1; i<=n; ++i){
fprintf(ofp, "%12d", rand());
if(i%6 == 0 || i == n)
fprintf(ofp, "\n");
}
void get_info(char *filename, int *n_ptr)
{
prinf("\n%S\n\n%s",
"This progam creates a file of random numbers.",
"How many random numbers would you like? "),
scanf("%d", n_ptr);
printf("\nIn what file would you like them? ");
scanf("%s", filename);
}
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;
}
Any help would be greatly appreciated!!!!!