Almighty_Chocobo
Member
I'm working on this program for my C class. Basically what it does is read a total of 10 variables from the keyboard, then stores them into a file named legs.txt. They are stored 2 to a line with a tab between them, and 5 total lines. Later on in the program I'm trying to recall these variables, 2 at a time(or one line at a time), to calculate the hypotenuse and print them to a different file, hypleng.out.
My problem though, is that when I'm recalling the variables from this legs.txt file, only the LAST 2 variables are fetched.
How do I make my program fetch the FIRST line(with only the first 2 variables), then erase that line so that I can calculate the next line when the program calls for it? The teacher made it explicitly clear that we are to ONLY save the legs as 2 integers, rather than leg1, leg2, leg3, leg4, leg5, etc...
Here's what I've got so far:
#include <stdio.h>
#include <math.h>
void main (void)
{
int leg1, leg2, number=1, x;
float hypotenuse;
FILE *infile, *outfile, *outfile_C;
infile = fopen ("legs.txt", "r");
outfile = fopen ("legs.txt", "w");
outfile_C =fopen ("HYPLENG.OUT", "w");
printf ("Input the values of the leg lengths\nfor five right triangles\n\n");
printf ("Use the tab button to seperate lines of the triangle\n");
printf ("Leg1\tLeg2\n");
/*scan the legs from keyboard, then store them in a file*/
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
fprintf (outfile_C, "Hypotenuse lengths of five triangles\n\n");
fprintf (outfile_C, "Triangle\tLeg 1\t\tLeg2\t\tHypotenuse\n");
fprintf (outfile_C, "number\tlength\tlength\tlength\n");
/*get values of legs from file and begin printing hypotenuses to screen*/
fscanf (infile, "%d %d", leg1, leg2);
x = pow(leg1,2)+pow(leg2,2);
hypotenuse=sqrt(x);
fprintf (outfile_C, " %d\t\t %d\t\t %d\t\t%.3f", number, leg1, leg2, hypotenuse);
/*repeat for next line*/
fscanf (infile, "%d %d", leg1, leg2);
x = pow(leg1,2)+pow(leg2,2);
hypotenuse=sqrt(x);
number++;
fprintf (outfile_C, "\n %d\t\t %d\t\t %d\t\t%.3f", number, leg1, leg2, hypotenuse);
}
My problem though, is that when I'm recalling the variables from this legs.txt file, only the LAST 2 variables are fetched.
How do I make my program fetch the FIRST line(with only the first 2 variables), then erase that line so that I can calculate the next line when the program calls for it? The teacher made it explicitly clear that we are to ONLY save the legs as 2 integers, rather than leg1, leg2, leg3, leg4, leg5, etc...
Here's what I've got so far:
#include <stdio.h>
#include <math.h>
void main (void)
{
int leg1, leg2, number=1, x;
float hypotenuse;
FILE *infile, *outfile, *outfile_C;
infile = fopen ("legs.txt", "r");
outfile = fopen ("legs.txt", "w");
outfile_C =fopen ("HYPLENG.OUT", "w");
printf ("Input the values of the leg lengths\nfor five right triangles\n\n");
printf ("Use the tab button to seperate lines of the triangle\n");
printf ("Leg1\tLeg2\n");
/*scan the legs from keyboard, then store them in a file*/
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
scanf ("%d %d", &leg1, &leg2);
fprintf (outfile, "\n%d\t%d", leg1, leg2);
fprintf (outfile_C, "Hypotenuse lengths of five triangles\n\n");
fprintf (outfile_C, "Triangle\tLeg 1\t\tLeg2\t\tHypotenuse\n");
fprintf (outfile_C, "number\tlength\tlength\tlength\n");
/*get values of legs from file and begin printing hypotenuses to screen*/
fscanf (infile, "%d %d", leg1, leg2);
x = pow(leg1,2)+pow(leg2,2);
hypotenuse=sqrt(x);
fprintf (outfile_C, " %d\t\t %d\t\t %d\t\t%.3f", number, leg1, leg2, hypotenuse);
/*repeat for next line*/
fscanf (infile, "%d %d", leg1, leg2);
x = pow(leg1,2)+pow(leg2,2);
hypotenuse=sqrt(x);
number++;
fprintf (outfile_C, "\n %d\t\t %d\t\t %d\t\t%.3f", number, leg1, leg2, hypotenuse);
}