I made several comments on this code, hope this helps. This code is for fun, and have it doing a lot of what I want it to, just not all. I want random generated to write to a txt file. I tried to use an array but that failed. I wanted to use an array because i am only passing one value. Which makes sense since the random generated function is an int.
I made the fprintf as a comment but hopefully soon it will be able to send the values to the txt file. After that I will tackle the function.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define HIGH 49
#define LOW 1
int random_generated()
{
static int randValue = 1;
static int randValue2= 0;
static int numberout;
int j=0,i=0;
srand(time(NULL));
while (i < 100) {
for (j=0; j<6; j++) { // loops 6 times resulting in the while loop producing 6 numbers
// that were produced at the sametime
do{
randValue= (rand() %(HIGH-LOW+1))+LOW; //generates random values
randValue2= (rand() %(HIGH-LOW+1))+LOW; //generates random values
}
while (randValue!=randValue2);
randValue = numberout;
return numberout; // will call this in datafile() and print its output to a .txt
} //sends one value but I want 6, do I need an Array?
// tried to use one but it failed, might have used it wrong
i++;
}
}
void datafile()
{
char file_name[81];
char *p;
int write_to_file;
int i;
FILE *fp;
printf("Enter a file name:");
fgets(file_name,sizeof(file_name),stdin);
p=strchr(file_name, '\n');
if ((p)) {
*p='\0';
}
fp =fopen(file_name,"r+");
if(fp==NULL)
{
printf("The file was not successfully opened\n");
printf("Please check if the file exists\n");
exit(1);
}
write_to_file = random_generated();
printf("%d",write_to_file);
//fprintf(fp, write_to_file); // this will hopefully soon write to a txt file with the output from
//random_generated...
fclose(fp);
}
int main(int argc, const char * argv[])
{
datafile();
random_generated();
return 0;
}