Thread: Problem with float and argv

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    38

    Problem with float and argv

    I need to be able to read floats from a .DAT file. However, I do not want to assume that all of the lines will strictly be floats (comment lines, etc., are quite common in .DAT files that I use).

    When I write the code to a file, the file contains the entire contents of my .DAT file correctly, but when I ask the program to display the contents of the array (buffer), it gives incorrect values. What am I doing wrong?

    Here's the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[]){
        FILE* dataFile;
        long file_size;
        float* buffer;
        size_t result;
        int i;
        
        dataFile = fopen (argv[1], "rb");
        if (dataFile==NULL){
           fputs ("File error", stderr);
           exit (1);
           }
    
        // obtain file size:
        fseek (dataFile, 0, SEEK_END);
        file_size = ftell (dataFile);
        rewind (dataFile);
    
        // allocate memory to contain the whole file:
        buffer = (float*) malloc (sizeof(float)*file_size);
        if (buffer == NULL){
           fputs ("Memory error", stderr);
           exit (2);
           }
    
        // copy the file into the buffer:
        result = fread (buffer, 1, file_size, dataFile);
        if (result != file_size){
           fputs ("Reading error", stderr);
           exit (3);
           }
    
        /* the whole file is now loaded in the memory buffer. */
        for (i=0; i<file_size; i++){
            printf("%f\n", buffer[i]);
            }
        getch();
        fclose (dataFile);
        free (buffer);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. filesize is the number of bytes, not the number of floats
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    How would I go about fixing that, then?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by DonFord81 View Post
    I need to be able to read floats from a .DAT file. However, I do not want to assume that all of the lines will strictly be floats (comment lines, etc., are quite common in .DAT files that I use).

    When I write the code to a file, the file contains the entire contents of my .DAT file correctly, but when I ask the program to display the contents of the array (buffer), it gives incorrect values. What am I doing wrong?
    In that case the file contents are really ASCII characters, besides the fread() is going to read a single item from the input file.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (comment lines, etc., are quite common in .DAT files that I use).
    So it's like this?
    Code:
    # this is the height
    123.456
    # this is the coordinate
    22.33 44.55
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Read a line into a temporary buffer, determine if it's valuable data or not (float vs comment), if it's good then you can add it to your "array". To properly account for the variances in the size of the data you need to store, you then might need to consider an initial malloc call for your array followed by repeated realloc calls or perhaps some other data structure (linked list) to store the floats.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGl Help
    By lucien in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2008, 03:12 PM
  2. rotating around object (looat)
    By jabka in forum Game Programming
    Replies: 13
    Last Post: 06-18-2008, 05:02 PM
  3. Weird thing convert float to binary
    By sara.stanley in forum C Programming
    Replies: 9
    Last Post: 02-13-2006, 09:17 AM
  4. The wall behind is showing over the wall in front. (Glut)
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2005, 04:50 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM