C/Error whilst updating a file
Expert: Zlatko - 1/5/2012
QuestionQUESTION: Hi again :)
I have encountered one final problem with another function in which I need to update my Inventory afterwards.
By default the file contains:
Total Number of Bees: 15
Total Number of Infected Bees: 5
Total Amount of Money: 1500
Total Amount of Honey Jars: 65
Total Amount of Beeswax: 0
However once I update it to increase the number of infected bees and decrease the number of total bees it just randomizes all of my numbers. Any idea how I can go around this?
Thanks :)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LINE_1 "Total Number of Bees: %d
"
#define LINE_2 "Total Number of Infected Bees: %d
"
#define LINE_3 "Total Amount of Money: %f
"
#define LINE_4 "Total Amount of Honey Jars: %d
"
#define LINE_5 "Total Amount of Beeswax: %d
"
#define FILENAME "Inventory.txt"
int main ()
{;
int Inv_NumofBees;
int Inv_InfectedBees;
float Inv_Money;
int Inv_HoneyJars;
int Inv_Wax;
FILE* file;
srand (time(NULL));
printf ("The number of bees that have been infected is %d.
", rand()%5);
srand (1);
file = fopen(FILENAME, "w");
if (file == NULL)
{
printf("Cannot open file %s for writing
", FILENAME);
return 0;
}
fprintf(file, LINE_1, Inv_NumofBees-rand);
fprintf(file, LINE_2, Inv_InfectedBees+rand);
fprintf(file, LINE_3, Inv_Money);
fprintf(file, LINE_4, Inv_HoneyJars);
fprintf(file, LINE_5, Inv_Wax);
fclose(file);
getchar();
getchar();
return 0;
}
ANSWER: Hi Stephane
You are getting random data in the file because you are not initializing the Inv_ variables. You should always initialize all your variables before using them. Don't assume that they start at 0.
int Inv_NumofBees = 0;
int Inv_InfectedBees = 0;
float Inv_Money = 0;
int Inv_HoneyJars = 0;
int Inv_Wax = 0;
Also notice that where you are doing
Inv_NumofBees-rand
it is not calling the rand function. To call the function you need
Inv_NumofBees-rand() /* notice the parentheses () */
The value of rand is the address of the rand function in memory. The value of rand() is the value returned by the function.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: For some reason I am still getting random data in my text file despite making the adjustments. This is the current state of the program just in case.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LINE_1 "Total Number of Bees: %d\n"
#define LINE_2 "Total Number of Infected Bees: %d\n"
#define LINE_3 "Total Amount of Money: %f\n"
#define LINE_4 "Total Amount of Honey Jars: %d\n"
#define LINE_5 "Total Amount of Beeswax: %d\n"
#define FILENAME "Inventory.txt"
int main ()
{
int Inv_NumofBees=0;
int Inv_InfectedBees=0;
float Inv_Money=0;
int Inv_HoneyJars=0;
int Inv_Wax=0;
FILE* file;
srand (time(NULL));
printf ("The number of bees that have been infected is %d.\n", rand()%5);
srand (1);
file = fopen(FILENAME, "w");
if (file == NULL)
{
printf("Cannot open file %s for writing\n", FILENAME);
return 0;
}
fprintf(file, LINE_1, Inv_NumofBees-rand());
fprintf(file, LINE_2, Inv_InfectedBees+rand());
fprintf(file, LINE_3, Inv_Money);
fprintf(file, LINE_4, Inv_HoneyJars);
fprintf(file, LINE_5, Inv_Wax);
fclose(file);
getchar();
getchar();
return 0;
}
AnswerEvery time you call rand() you get a new random number. I thought that was what you wanted. I guess you actually want the same number used throughout. Of course that makes sense. Save the value like this:
int infected;
infected = rand() % 5;
Then use the infected variable instead of calling rand() in your printf, and your fprintf