I have this rather large project to do. Too big to post here. I have to write results from a rock drilling exercise. I have a structure called rock with 6 variables. Everything goes as planned and i write all the details to a text file including the present time plus 8 hours for a due date for drilling the rock. My .txt file looks like this which is as i want it
1001 11 frank 1 1 Sat Feb 28 04:23:49 2015
I save my details i then open the program again and save again and my text file is now like this.
1001 11 frank 1 1 Sat
0 0 Feb 28 4 :23:49
The time function seems to be causing the problem. Any ideas
void load_master_data()
{
struct rover b;
struct rock c;
int i;
// open master file for reading and writing
masterFile = fopen("Rover_Master.txt", "r+");
// create master file if it doesn't exist
if (masterFile == NULL)
{
FILE * temp;
// open file for writing (creates file)
temp = fopen("Rover_Master.txt", "w");
fclose(temp);
masterFile = fopen("Rover_master.txt", "r+");
if (masterFile !=NULL)
printf("Master file created successfully!\n");
else
printf("Error while creating master file!\n");
}
while (!feof(masterFile))
{
fscanf(masterFile, "%d", &b.rover_number);
fscanf(masterFile, "%s", &b.rover_name);
++totalRovers;
}
--totalRovers;
fseek(masterFile,0,0);
rovers = (struct rover *) malloc(totalRovers*sizeof(struct rover));
// reading data about branches
for (i=0; i<totalRovers; ++i)
{
fgetpos(masterFile, &rovers[i].cursorPos);
//++Branches[i].cursorPos;
fscanf(masterFile, "%d", &rovers[i].rover_number);
fscanf(masterFile, "%s\n", &rovers[i].rover_name);
}
masterFile2 = fopen("Rock_Master.txt", "r+");
if (masterFile2 == NULL)
{
FILE * temp1;
// open file for writing (creates file)
temp1 = fopen("Rock_Master.txt", "w");
fclose(temp1);
masterFile2 = fopen("Rock_master.txt", "r+");
if (masterFile2 !=NULL)
printf("Master file created successfully!\n");
else
printf("Error while creating master file!\n");
}
while (!feof(masterFile2))
{
fscanf(masterFile2, "%d", &c.rock_rover_number);
fscanf(masterFile2, "%d", &c.rock_number);
fscanf(masterFile2, "%s", &c.geoligist);
fscanf(masterFile2, "%d", &c.drilling_candidate);
fscanf(masterFile2, "%d", &c.rock_completed);
fscanf(masterFile2, "%s", &c.due_date);
++totalRocks;
}
--totalRocks;
fseek(masterFile2,0,0);
rocks = (struct rock *) malloc(totalRocks*sizeof(struct rock));
for (i=0; i<totalRocks; ++i)
{
fgetpos(masterFile2, &rocks[i].cursorPos);
fscanf(masterFile2, "%d", &rocks[i].rock_rover_number);
fscanf(masterFile2, "%d", &rocks[i].rock_number);
fscanf(masterFile2, "%s", &rocks[i].geoligist);
fscanf(masterFile2, "%d", &rocks[i].drilling_candidate);
fscanf(masterFile2, "%d", &rocks[i].rock_completed);
fscanf(masterFile2, "%s", &rocks[i].due_date);
}
}