Writing to a file

This is a discussion on Writing to a file within the C++ Programming forums, part of the General Programming Boards category; I'd like to input the values that I compute, in a loop, to a file that I've already read from. ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    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) );
    }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Thanks for the help on previous posts. I figured this one out on my own. Good luck programming.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21