Thread: File Data Saving

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    File Data Saving

    Hi, I'm a beginner with C, and as part of a piece of University Coursework (I'm on a physics course), and there's a part where I have to test if there are the right number of data entries and if the data entries are formatted correctly, and I'm unsure of how to test this. The coursework is to make a Pendulum and this particular exercise was to modify the pendulum program so that the time and theta values are saved to a file when the program has finished running. My program is included in an attachment. Thanks

    Code:
    #include <stdio.h>
    int main(void)
    {
     int i;           //this is the step counter
     float theta[10000];
     theta[0]=0;   // this is the initial value for the angle
     float omega=0.1; // this is the initial value for the angular speed.
     float time[10000];    // this is the initial time.
     float dt=0.01;   //this is the time step. This is how much the time increases every step.
     float length=2.39; //this is the length of the pendulum
     float g=9.81;     //this is the value for gravitational acceleration.
     int steps=10000;   //number of steps. The number of steps is 100.
     time[0]=0;
    //while(theta<6.28 && theta>-6.28)
    // {
    for(i=1 ;i<steps; i=i+1)
     {
         time[i] = time[i-1]+dt;
         printf("Step Number is %i and Time equals %f\n",i,time[i]);
         omega=omega-(g/length)*dt*sin(theta[i-1]);
         theta[i]= theta[i-1]+omega*dt; // calculation of the angle, theta
         printf("Time equals %f, ",time);
         printf("angular speed is %f, ", omega);
         printf("and theta equals %f \n",theta);
    
         if (theta[i]>3.14 || theta[i]<-3.14)
         {
             i=steps;
         }
     }
    
    if (theta[10000]=omega*time[10000])
            printf("The value of theta is correct\n");
    else
            printf("The value of theta is incorrect");
    // }
     system("PAUSE");
     {
         FILE *time, *theta; //variables to hold the file name
         int i,x;
             time= fopen("testin.txt","r"); //opening file for reading
             theta= fopen("testout.txt","w"); //opening file for writing
        for (i=0;i<10;i++)
        {
            fscanf(time,"%f",&x); //reading values from file
            fprintf(theta, "%f %f\n",x,x*x);
        }
        fclose(theta); //close file
        fclose(time); //close file
     }
    }
    Attached Files Attached Files
    Last edited by Salem; 02-20-2020 at 11:11 PM. Reason: inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (theta[10000]=omega*time[10000])
    1. These array elements don't exist.
    The last subscript in an array of 10000 elements is 9999 (it's always N-1 for an array[N])

    Line38 onwards made no sense.
    - You're using the same variable names for file handles as you used for pendulum calculations.
    - You're just making a table of x2

    Perhaps
    Code:
    FILE *out = fopen("pendulum.txt","w");
    for ( i = 0 ; i < size ; i++ ) {
      fprintf("%d: %f %f\n", i, time[i], theta[i]);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    Checking if the data is saved correctly.

    Thank you very much. But do you know how to check if the data is saved correctly? Like how to check if there are the right number of data entries and to check if these are formatted correctly?

  4. #4
    Registered User
    Join Date
    Feb 2020
    Posts
    7
    Like how to check if the data is saved to a file when the program is finished running?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Like how to check if the data is saved to a file when the program is finished running?
    Well you could check the return result of every single fprintf call.

    On some level, you have to assume that some things just work.

    Otherwise, you end up with an infinite recursion of
    - how do I check the data was saved
    - how do I check the check was correct
    - how do I check the check of the check was correct
    ....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and Saving data from file
    By MrPecanha in forum C Programming
    Replies: 12
    Last Post: 06-10-2016, 08:57 AM
  2. Problems saving read file data into a structure
    By jcmoney in forum C Programming
    Replies: 3
    Last Post: 11-19-2012, 05:37 AM
  3. Replies: 20
    Last Post: 06-07-2007, 06:38 PM
  4. saving data into a file
    By afrm in forum C Programming
    Replies: 4
    Last Post: 05-22-2006, 09:54 AM
  5. Replies: 2
    Last Post: 06-16-2005, 10:03 AM

Tags for this Thread