-
Writing to a file
I'd like to input the values that I compute, in a loop, to a file that I've already read from. I've opened it as a read/write file. I can't seem to make it work, though it looks as if it should. Any ideas?
Code:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define L1 1000
#define L2 1000
void eliminateEnters (char s1[]);
void DetermineAngle( struct Scara * );
void CalcMotorPulses ( struct Scara *s );
double CheckSum ( struct Scara *s );
struct Scara
{
char name[256];
double x;
double y;
double z;
double joint1;
double joint2;
double joint3;
int motor1;
int motor2;
int motor3;
double checkSum;
};
FILE *in;
void main()
{
char fileStr[30];
char buffer[256];
int i;
int j;
int p;
struct Scara pts[10];
double sum;
printf("\nPlease enter the name of the file that you'd like to read points from: ");
gets(fileStr);
in = fopen(fileStr, "a+");
if (in == NULL)
{
printf("\nFile Error......");
exit(1);
}
printf("\n%s openend for reading", fileStr);
/*fprintf(in,"\nX-pt Y-pt Z-pt Joint1 Joint2 Joint3 Motor1 Motor2 Motor3 CheckSum");
fprintf(in,"\n=== === === ===== ===== ===== ===== ===== ===== ========");*/
printf("\n\nX-pt Y-pt Z-pt Joint1 Joint2 Joint3 Motor1 Motor2 Motor3 CheckSum");
printf("\n=== === === ===== ===== ===== ===== ===== ===== ========");
j=0;
while(j<10)
{
i=0;
while( (fgets(buffer, 256, in) != NULL) )
{
eliminateEnters(buffer);
if(i == 0)
strcpy(pts[j].name, buffer);
if(i == 1)
pts[j].x = atof(buffer);
if(i == 2)
pts[j].y = atof(buffer);
if(i == 3)
pts[j].z = atof(buffer);
//printf( "i=%d, buffer=%s\n", i, buffer );
i++;
if ( i > 3 )
break;
}
DetermineAngle(&pts[j]);
CalcMotorPulses (&pts[j]);
sum = CheckSum (&pts[j]);
printf("\n\n%5.2lf %5.2lf%5.2lf %5.2lf %5.2lf %5.2lf %3d %3d %3d %5.2lf", pts[j].x, pts[j].y, pts[j].z, pts[j].joint1, pts[j].joint2, pts[j].joint3, pts[j].motor1, pts[j].motor2, pts[j].motor3, sum);
//fprintf(in,"%5.2lf %5.2lf%5.2lf %5.2lf %5.2lf %5.2lf %3d %3d %3d %5.2lf", pts[j].x, pts[j].y, pts[j].z, pts[j].joint1, pts[j].joint2, pts[j].joint3, pts[j].motor1, pts[j].motor2, pts[j].motor3, sum);
j++;
}
for(p=0;p<10;p++);
fprintf(in,"%5.2lf %5.2lf%5.2lf %5.2lf %5.2lf %5.2lf %3d %3d %3d %5.2lf", pts[p].x, pts[p].y, pts[p].z, pts[p].joint1, pts[p].joint2, pts[p].joint3, pts[p].motor1, pts[p].motor2, pts[p].motor3, sum);
}
/*********************************************************************
* Name: eliminateEnters
* About: eliminates enters from an array so that only numbers, periods, or the NULL character occurs
* Arguments (character array)
* Returns (none)
*********************************************************************/
void eliminateEnters (char s1[])
{
int i=1;
while(s1[i] != '\0')
{
if(s1[i] == '\n')
s1[i] = '\0';
i++;
}
}
#include <math.h>
void DetermineAngle( struct Scara *s )
{
double B, Beta, anglealpha, Angleone, Angletwo;
B = sqrt((s->x * s->x)+(s->y * s->y));
Beta = atan2(s->y,s->x);
anglealpha = acos(((L2*L2) - (B*B) - (L1*L1))/ (-2*B*L1));
Angleone = Beta + anglealpha;
Angletwo = atan2((s->y-(350*sin(Angleone))),(s->x-(350*cos(Angleone))));
(s->joint1 = Angleone * (180/3.1416));
(s->joint2 = Angletwo * (180/3.1416));
(s->joint3 = s->z);
}
void CalcMotorPulses ( struct Scara *s )
{
s->motor1 = s->joint1 * 800;
s->motor2 = s->joint2 * 800;
s->motor3 = s->joint3 * 400;
if(s->motor1 < 0)
s->motor1 = s->joint1 * 800 * -1;
if(s->motor2 < 0)
s->motor2 = s->joint2 * 800 * -1;
if(s->motor3 < 0)
s->motor3 = s->joint3 * 400 * -1;
}
double CheckSum ( struct Scara *s )
{
return ( (s->joint1)+(s->joint2)+(s->joint3)+(s->motor1)+(s->motor2)+(s->motor3)+(s->x)+(s->y)+(s->z) );
}
-
Thanks for the help on previous posts. I figured this one out on my own. Good luck programming.
-
Don't use void main: http://cpwiki.sourceforge.net/Void_main
Don't use gets: http://cpwiki.sourceforge.net/Gets
And try wrapping some of your long lines.
-
And unless you have REALLY good reason [1], don't add includes in the middle of the code. It confuses everyone, and doesn't make anything better. (And in this case, you have already included <math.h> at the top of the file, so the second include of it is meaningless.
[1] The only reason I can think of is when you include a file containing for example data that forms a table of some sort. But this should be done as "early" as possible in the file.
--
Mats