Thread: problem reading numbers in a txt file

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Question problem reading numbers in a txt file

    I'm trying to read n lines of numbers with in each of the lines 6 numbers. These numbers(each line) are the coordinates of a triangle like:

    4 ->number of triangles(lines)
    x1 y1 x2 y2 x3 y3
    x1 y1 x2 y2 x3 y3
    x1 y1 x2 y2 x3 y3
    x1 y1 x2 y2 x3 y3

    My input file looks like this:

    Code:
    4
    0 0 0 4 3 0
    3 3 6 1 0 0
    2 3 1 1 0 9
    3 4 5 7 1 1
    And my readTriangle.c :

    Code:
    #include "triangle.h"
    #include "point.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	double x1,y1,x2,y2,x3,y3;	
    	int i, a;
    	Point p1, p2, p3;
    	
    	FILE *bestand;
    
    	bestand = fopen("bestand.txt", "r");
    	if(bestand == NULL)
    	{
    		printf("kan het bestand niet openen!\n");
    		exit(1);                        
    	}
    
    	fscanf(bestand, "%d", &a);           // read the first number(number of triangles)
    
    	for(i = 0; i < a; i++)				// repeat this until you're done with all the lines
      	{   
    		fscanf(bestand, "%f", &x1);			
    		fscanf(bestand, "%f", &y1);				
    		fscanf(bestand, "%f", &x2);				
    		fscanf(bestand, "%f", &y2);				
    		fscanf(bestand, "%f", &x3);				
    		fscanf(bestand, "%f", &y3);	
    		
    		p1 = makePoint(x1, y1);
    		p2 = makePoint(x2, y2);
    		p3 = makePoint(x3, y3);
    	
    		Triangle t = makeTriangle(p1, p2, p3);
    
    		//if(isEmpty(t)) 
    		//{
    			//printf("deze punten maken geen driehoek!\n");
    		//} 
    		//else
    		//{	
    		
    			printf("de driehoek: "); showTriangle(t); 
    			printf("heeft een oppervlakte van %f \n", area(t));
    		//}
    	}
    	fclose(bestand);
    	return 0;
    }
    Now my problem is that my program thinks these number are all 0. Of course the area is also 0.

    normally(without the reading) my area function calculates different area's each time(very big numbers) . But when I test it seperately It works fine!!!

    I'm using makeFile to compile

    does anybody know how I can fix it?

    Thanks,
    Nima

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't read a double with %f. You can read a float with %f, or a double with %lf.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    thanks tabstop :P

    There's another question(sorry i'm an absolute beginner)

    why doesn't abs work in my function?

    I have already #include <math.h>

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no abs in math.h. There is fabs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in a txt file into a list
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 12-25-2007, 01:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM