0

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);
    }
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
niallo27
  • 215
  • 1
  • 2
  • 8
  • 2
    Please post the function, it;s impossible to help you otherwise, and explain the structure of the file line. – Iharob Al Asimi Feb 27 '15 at 20:32
  • Post the function where you are performing this operation – tourniquet_grab Feb 27 '15 at 20:33
  • the due_date is fine if i input friday or something, but i have another function which completes the rock and this sets the due_date to the present time which saves fine but the present time can not be loaded back into next time i run. I am new to programming so go easy on me. – niallo27 Feb 27 '15 at 20:41
  • You never check the return value of any function and also [Don't cast the result of `malloc()`](http://stackoverflow.com/a/605858/1983495) yet you must check it's return value against `NULL`, also check the result of `fscanf()`. – Iharob Al Asimi Feb 27 '15 at 20:57
  • It works as expected until i introduce the present time. can i return just the time without the date. If i dont set due_date to the present time and just leave it as friday or something it works as expected. – niallo27 Feb 27 '15 at 21:01
  • @niallo27 wroks as expected doesn't mean it's correct to ignore everything and just make the program work, when it eventually fails then you won't know why.... Also, what Operating system are you using? – Iharob Al Asimi Feb 27 '15 at 21:07
  • I'm on windows. I agree with you but I'm fairly new to the course and i am learning as i go on so any help and advice is very much appreciated. – niallo27 Feb 27 '15 at 21:12

1 Answers1

0

You cannot input a date in this format with this code:

    fscanf(masterFile2, "%s", &rocks[i].due_date);

fscanf will scan a single word from masterFile2, the date format contains embedded spaces: Sat Feb 28 04:23:49 2015. You should either use fgets or fscanf with a character class.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks, could you give me an example i cant just replace fscanf with fgets can i. – niallo27 Feb 27 '15 at 21:10
  • For example `fgets(rocks[i].due_date, sizeof rocks[i].due_date, masterFile2);` will read the rest of the input line without overflowing character array `rocks[i].due_date` (btw many `&` in your code are useless and incorrect). The structure definition is not provided, I assume `rocks[i].due_date` is a char array. Alas, you will have to deal with the `'\n'` at the end of the array. – chqrlie Feb 27 '15 at 21:19
  • It works when i delete the file but once it try's to read it on startup the program crashes – niallo27 Feb 27 '15 at 21:24
  • Use a debugger to see where it crashes – chqrlie Feb 27 '15 at 21:30
  • I got it returning the date and time but it also returns rubbish after it 1001 1 john 1 1 Sat Feb 28 05:32:56 2015 0 0 0 0 0 0 0 0 – niallo27 Feb 27 '15 at 21:35
  • edit your question to update the code and also produce the actual input file contents – chqrlie Feb 27 '15 at 21:42
  • The ones on arrays. arrays automatically decay as pointers when passed as function arguments. Keeping the `&` does not hurt either: you pass the same address, but the type is incorrect. – chqrlie Feb 27 '15 at 22:12
  • Thanks for your help. I very much appreciate it. – niallo27 Feb 27 '15 at 22:15