Thread: Reading doubles from text file

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Angry Reading doubles from text file

    Ok so I have some code:

    Code:
    void loadBuildingImagesInfo()
    {
    	int count, number;
    	double numbertest;
    	FILE *infile;
    	infile = fopen("BuildingImages.txt", "r");
    	for(count = 0; count < 16; count++)
    	{
    		for(number = 0; number < 32; number++)
    		{
    			fscanf(infile, "%f", &numbertest);
    			printf("%f - ", numbertest);
    			buildingImages[count][number] = numbertest;
    		}
    	}
    }
    In the textfile I have:
    0.55 0.085 0.55 0.16 0.71 0.16

    (this is just a few, having 512 numbers on this post is a bit absurd)

    However, I cannot seem to get these numbers out, I tried print whats being read out, to test, and its just displaying some random long negative number, this is the first one:

    -92559604195412477000000000000000000000000000000000 000000000000.000000

    I have no idea how to go about this. both the array is a double and so is numbertest. so not sure. I have tried changing the %f to things like %1.2f etc. but no difference,

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is not possible to read a double with scanf using %f. Fortunately, it is possible using %lf.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Quote Originally Posted by tabstop View Post
    It is not possible to read a double with scanf using %f. Fortunately, it is possible using %lf.
    Ooooo Thanks, never been taught that!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    never been taught that!
    It is time to learn using C-reference... for example msdn
    http://msdn2.microsoft.com/en-us/lib...hh(VS.71).aspx
    http://msdn2.microsoft.com/en-us/lib...9d(VS.71).aspx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM