Writing to a text file

I have made a program which simulates a pendulum, I need the values to output to a document but I'm getting a couple of errors. Here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
      float omegaValue;
      float thetaChange;
      float omegaGravValue;
      float thetaValue;
      float gravity = 9.81;
      float length = 5.15;
      int answer;
      FILE *fp_out;
      char Y;
      
      printf("Enter a value for omega\n");         //finding value for omega
      scanf("\n%f", &omegaValue);
      printf("Omega=%f\n" ,omegaValue); 
           
      int i;                //step counter
      float theta = 0;      //initial value for angle
      float omega = omegaValue;    //initial value for angular speed
      float time = 0;       //initial time
      float dt = 0.01;      //time step
      int steps = 100;      //number of steps
      for(i=0; i<steps; i++) {
               time = time + dt;
               omegaGravValue = omegaValue-(gravity/length)*dt*sin(thetaValue);
               thetaChange = theta + omegaGravValue*dt;
               thetaValue = theta+thetaChange*i;
               printf("Time equals %f. Theta equals %f. Omega equals %f\n",time,thetaValue,omegaGravValue);
      }
      if(thetaValue=omegaValue) {
                                printf("Theta and Omega are equal\n");
                                }
                                else {
                                     ("Theta and Omega are not equal\n");
                                }
      printf("Do you want to write this data to a file? Y/N");
      scanf("\n%c", &answer);
      if(answer == 'Y') {
                printf("Writing to file...");
                fp_out=("testout.txt","w");
                for (i=;i<100;i++)
                {
                    fprintf(fp_out,"%f %f\n", time,thetaValue);
                    }
                fclose(fp_out);
                }
                else {}
                
                    
      
      
      system("PAUSE");
}         
      


There errors are:
Line 44: cannot convert 'const char[2]' to 'FILE*' in assignment
Line 45: expected primary-expression before ';' token
The line:
 
fp_out=("testout.txt", "w");


Should be:
 
fp_out=fopen("testout.txt", "w");


Should be noted, though, this is C, not C++.
Okay, I should move a different forum then?
Also, I've just done what you've said and it is writing to the file. Just not the right values. Its just the same two values over and over again...

Edit:
I think what it is doing is just writing the end values of the for loop 100 times, instead of all of the values calculated in the for loop
Last edited on
Yeah, looking at your code you'll be writing the same values over and over.

You can do one of two things:
1) Move your write function to inside the loop where each value is calculated and automatically write to a file, rather than ask the user.

2) Store the results in an array(s), ask the user if they want to print, then iterate through the array(s), printing out each element as you go.
I need to do it as an array I think... because don't want the program to ask everytime if I want to write to a file!

How would I make it store in an array?
Well it looks like you were trying to print two float values to the file.

So, you could have two arrays, each storing floats. Remember, an array is just a list of the same data type. ( http://www.cprogramming.com/tutorial/c/lesson8.html )

However, a better approach would be to create a struct. The struct would be a container for time and theta. ( http://www.cprogramming.com/tutorial/c/lesson7.html )

I would opt for the latter approach. Create a struct, containing a float for time and a float for thetaValue. Then, in main, you'd create an array of these structs (make sure the size of your array matches your 'steps' variable, which, by the way, may be better as a constant). Then, when you do your calculations, just populate the corresponding element of the array.

Later on, you can loop through the array and print them out.
I'm meant to complete this as an array. Its part of some work for school!

I've managed to get the time to store into the file as an array, but the isn't working.

This is what I've done so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
      float omegaValue;
      float thetaChange;
      float omegaGravValue;
      float thetaValue;
      float gravity = 9.81;
      float length = 5.15;
      int answer;
      FILE *fp_out;
      char Y;
      
      printf("Enter a value for omega\n");         //finding value for omega
      scanf("\n%f", &omegaValue);
      printf("Omega=%f\n" ,omegaValue); 
           
      int i;                //step counter
      float theta = 0;      //initial value for angle
      float omega = omegaValue;    //initial value for angular speed
      float time[100];       //initial time
      time[0]=0;
      float dt = 0.01;      //time step
      int steps = 100;      //number of steps
      for(i=0; i<steps; i++) {
               time[i] = time[i-1] + dt;
               omegaGravValue = omegaValue-(gravity/length)*dt*sin(thetaValue);
               thetaChange = theta + omegaGravValue*dt;
               thetaValue = theta+thetaChange*i;
               printf("Time equals %f. Theta equals %f. Omega equals %f\n",time[i],thetaValue,omegaGravValue);
      }

      printf("Do you want to write this data to a file? y/n\n");
      scanf("\n%c", &answer);
      if(answer == 'y') {
                printf("Writing to file...\n");
                fp_out=fopen("testout.txt","w");
                for(i=0;i<100;i++){
                    fprintf(fp_out,"%f %f\n",time[i],thetaValue);
                    }
                fclose(fp_out);
                }
                else {}
                
                    
      
      
      system("PAUSE");
}         
      


I turned the time into an array and added the [i] and the [i-1] to the calculations. But when I try with thetaValue, it just comes up with #0NQAND# or something like that
Last edited on
I'm pretty certain you don't want this:
1
2
 for(i=0; i<steps; i++) {
               time[i] = time[i-1] + dt;


Think about what's happening there. Can you see why it's a bad idea?

You probably want:
1
2
3
4
5
6
7
8
9
float time = 0;
float timeArray[steps];

. . .

for(i=0; i<steps; i++)
{
   time += dt;
   timeArray[i] = time;
I see that's neater but now its not writing to the text document
It's not just neater, it's necessary.

Imagine the first iteration of your loop, when i = 0. In your code, you access the array element i - 1. In this iteration, i - 1 is -1 and is an illegal index of your array.

Also that's for your first for loop, when you're assigning the time to the array.

You'd write the file by using the line you have in your second loop.

Though you probably want something similar for thetaValue. Otherwise, when you write out to the file, thetaValue will always be the same.

If you do it this way, you have the problem of two concurrent arrays. They can be troublesome, especially if your project grows. Structs are the way forward with this one. :-)
Topic archived. No new replies allowed.