Program isnt writing to the file correctly

I am reading data points into a file and then reading the file for poi

Page 1 of 1

4 Replies - 1181 Views - Last Post: 03 October 2008 - 07:39 AM Rate Topic: -----

#1 vandervander15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-August 08

Program isnt writing to the file correctly

Post icon  Posted 02 October 2008 - 10:34 PM

ok so i have a program thats supposed 2 take datapoints entered by the user and write them to a text file and then read the text file and calculate the distances between each point, then the average distance between each point. only problem is, nothing is being written to the file resulting in a non terminating loop while its supposed to be reading the points from the file.

heres the code. thanks in advance for the help!

#include <stdio.h>
#include <math.h>
#define READFILE "points5.txt"
#define WRITEFILE "points5.txt"

	int main()
   {
	  double x;
	  double y;
	  double xwrite;
	  double ywrite;
	  double distanceSum = 0;
	  double x0 = 0;
	  double y0 = 0;
	  double distance = 0;
	  double count = 0;
	  double avgdistance;
	  
   	
	  FILE *writefile,
			*readfile;
	  writefile = fopen("points5.txt", "w"); 
	  do
	  {
		 printf("Enter X and Y Coordinates (Enter 0 0 to stop): ");
		 scanf("%lf %lf", &xwrite, &ywrite);
		 fprintf(writefile, " %.2f %.2f \n", xwrite, ywrite);
	  }while ( xwrite != 0, ywrite != 0 );
	  
	  printf("	  Point 1			   Point 2			 Distance\n");
	  readfile = fopen("points5.txt", "r");
	  x = 1;
	  y = 1;
	  while(x != 0, y != 0)
	  {
		 fscanf(readfile, " %lf %lf ", &x, &y);											  // Read X and Y
		 distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0)));							 // Calculate distance between points
		 printf(" (%5.2f , %5.2f )	 (%5.2f , %5.2f )	 %8.2f\n", x0, y0, x, y, distance);  // display distance between two points and those 2 points
		 x0 = x;																		  // store x as x0
		 y0 = y;																		  // store y as y0
		 distanceSum += distance;													// calculate total distance traveled
		 count += 1;
	  }
	  avgdistance = distanceSum / count;										 
	  printf("\nTotal Distance is %9.2f\n", distanceSum);
	  printf("\nThe number of points entered is %4.1f\n", count);
	  printf("\nThe Average Distance Between Points is %9.3f", avgdistance);
	  return 0;
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Program isnt writing to the file correctly

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: Program isnt writing to the file correctly

Posted 02 October 2008 - 11:46 PM

You are making this way to hard on yourself. Try implementing streams into your code. There purpose is to work with text files. Very easy and efficient to use. After all that's why they were invented. Just google ---> C++ streams. The following is syntax for streams to get you started.

#include <fstream>//Heres the library.
#include <iostream>//For system pause.
using namespace std;//For the >> and << operators.

ifstream testInStream;//the ifstream stands for input file stream meaning a stream variable that is designed to open
//a passportal from your text file to the program at hand. The following is how you open the passportal.
ofstream testOutStream;


testInStream.open("testInFile.txt");
testOutStream.open("testOutFile.txt");//This opens a portal to load data to a file.

//Use the following to see if the file portal was opened.
		if (testInStream.fail( ))
		{
			cout << "***FILE FAILED TO OPEN!";
			cout << " TERMINATING PROGRAM***\n";
			system("PAUSE");
			exit(1);
		}

//From here you have a couple of options. The following is the beginning of a loop that goes until the end of the file.

while (! testInStream.eof( ))//While the text file has not been completely read into this program.
{
   //Do something here. For example use ---> testInStream.get(next); next is a variable you created to store the 
   //input data being streamed in from your text file.
}

//Now use your output stream to connect to a file you wish to load (output) data to.
//For example you could use the next variable along with strings to output to a document.
//You can even use the >> and << operators to do the work for example...
testOutStream << n << " is the identification number for " << userName;//User name can be a string.


Streams are exactly what the name implies. They are a pathway a specified channel also known as a passportal that allows data to flow from one file pool to another file pool. After all files are just pools of data. Allright good luck. B)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Program isnt writing to the file correctly

Posted 03 October 2008 - 04:27 AM

Perhaps this assignment must be done in pure C, so no C++?

Anyway, try closing the file after you've written all the values to the file, and before you start reading them.
Was This Post Helpful? 1
  • +
  • -

#4 vandervander15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 27-August 08

Re: Program isnt writing to the file correctly

Posted 03 October 2008 - 07:34 AM

Thank you both for the help. If it had been in C++ I am sure I would have done streams, however it is a purely C assignment. JackOfAllTrades thank you for the advice about closing the file. I now get values but the last value in the output i do not want but cant seem 2 be able 2 embed a correct if/else statement into the code to make it break appropriately

when the while loop code reads this

 while(x  != 0, y != 0)
	  {
			fscanf(readfile, " %lf %lf ", &x, &y);											  // Read X and Y					   
			distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0)));								// Calculate distance between points		   
			printf(" (%5.2f , %5.2f )	 (%5.2f , %5.2f )	 %8.2f\n", x0, y0, x, y, distance);  // display distance between two points and those 2 points
			x0 = x;																		  // store x as x0
			y0 = y;																		  // store y as y0
			distanceSum += distance;													// calculate total distance traveled
			count += 1;
		 
	  }



i now get this output

Enter X and Y Coordinates (Enter 0 0 to stop): 2 1
Enter X and Y Coordinates (Enter 0 0 to stop): 4 3
Enter X and Y Coordinates (Enter 0 0 to stop): 6 8
Enter X and Y Coordinates (Enter 0 0 to stop): 10 14
Enter X and Y Coordinates (Enter 0 0 to stop): 0 0
Point 1 Point 2 Distance
( 0.00 , 0.00 ) ( 2.00 , 1.00 ) 2.24
( 2.00 , 1.00 ) ( 4.00 , 3.00 ) 2.83
( 4.00 , 3.00 ) ( 6.00 , 8.00 ) 5.39
( 6.00 , 8.00 ) (10.00 , 14.00 ) 7.21
(10.00 , 14.00 ) ( 0.00 , 0.00 ) 17.20

Total Distance is 34.87

The number of points entered is 5.0

The Average Distance Between Points is 6.973

when the while statement reads this with the if statement

	  while(x , y)
	  {
		 fscanf(readfile, " %lf %lf ", &x, &y);											  // Read X and Y
		 if (x = 0, y = 0)
			break;
		 else 
		 {  
			distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0)));								// Calculate distance between points		   
			printf(" (%5.2f , %5.2f )	 (%5.2f , %5.2f )	 %8.2f\n", x0, y0, x, y, distance);  // display distance between two points and those 2 points
			x0 = x;																		  // store x as x0
			y0 = y;																		  // store y as y0
			distanceSum += distance;													// calculate total distance traveled
			count += 1;
		 }
	  }



and i get this output

Enter X and Y Coordinates (Enter 0 0 to stop): 2 1
Enter X and Y Coordinates (Enter 0 0 to stop): 4 3
Enter X and Y Coordinates (Enter 0 0 to stop): 6 8
Enter X and Y Coordinates (Enter 0 0 to stop): 10 14
Enter X and Y Coordinates (Enter 0 0 to stop): 0 0
Point 1 Point 2 Distance
( 0.00 , 0.00 ) ( 0.00 , 0.00 ) 0.00

Total Distance is 0.00

The number of points entered is 1.0

The Average Distance Between Points is 0.000

that tells me its breaking too soon but i dont know why.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Program isnt writing to the file correctly

Posted 03 October 2008 - 07:39 AM

This is wrong:
if (x = 0, y = 0)


Remember, = is for assignment, == is for comparison. And the comma operator is inappropriate here as well, you want:

if (x == 0 && y == 0)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1