Thread: reading int's and floats...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question reading int's and floats...

    I need to read integers and floating point data from a file and calculate the mean and variance. I know how to perform the calculations, however I don't know where to start when it comes to reading in this data correctly. Any examples would be appreciated.
    Thanks!
    flight

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    It depends on how the file is formatted, but fscanf is a good option:
    Code:
    if (fscanf(fd, "%d %lf", &i, &fp) == 2) {
      /* Good */
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink sounds good but what do you think of

    using sscanf. The data file is only a sinle column of 10 integers and floats. Let me know if you think sscanf will work.
    Thanks!
    flight

  4. #4
    root
    Join Date
    Sep 2003
    Posts
    232
    >Let me know if you think sscanf will work.
    Since sscanf has trouble reading from files, you'd have to use fgets first, but it should work. Of course, reading ten items with sscanf makes for an annoyingly long format string, so strtod might be a better choice.
    Code:
    char *endp;
    char *s;
    double d;
    
    if (fgets(buffer, sizeof buffer, fd) != NULL) {
      for (s = buffer; *s != '\0'; s = endp) {
        d = strtod(s, &endp);
        /* Use d */
      }
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink still need help...

    The data file looks like this:
    132.34
    100
    235
    7859.03
    444.31
    332
    298
    545.77
    676
    33

    I need to read this data in and then calculate the mean and variance of the data.
    I'm just not sure which function in C is the most appropriate one to do read the data and store it in array 'value[m]'. I was thinking of using sscanf but I cant seem to get it to process the data correctly. Here's my code.

    Code:
    int main()
    {
    
     char file_name[100], line1[10] ;
     float b1[10], value[10] ;
     int count ;
     int update_counter, m, prework2a = 0;
     FILE *inp1 ;
    
       // prompt the user
       printf("Enter Filename : ")  ;
    
       // read the keyboard input
       fgets(file_name, sizeof(file_name), stdin);
    
       //remove the newline character from the filename
       file_name[strlen(file_name) - 1] = '\0' ;
    
       // open input file
       inp1 = fopen(file_name, "r") ;
       if(inp1 == NULL)	
       {
         fprintf(stderr, "can't open %s\n", file_name) ;
         exit(EXIT_FAILURE) ;
       }
    
       //read in the data from the file and store
       //in array value[m]
       for (m=1 ; m<=10 ; m++) 
       {
          if(fgets(line1, sizeof(line1), inp1) != NULL) 		{
         {
              sscanf (line1, "%d",b1) ;
              value[m] = b1[m] ;
              printf("value = %d \n", value[m]) ;
          }
       }
       fclose(inp1) ;
       return 0 ;
    }

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    not sure if my last post

    got in or not. please see last post for code and data set.
    Thanks!
    Flight

  7. #7
    root
    Join Date
    Sep 2003
    Posts
    232
    We seem to be going in circles:
    Code:
    while (fscanf(inp1, "%f", &value[m]) == 2 && m < 10)
      m++;
    That's really all you need now that I've seen the file. If you want you can differentiate between int and float, but there's really little point. An int cast to float is still the same value with no precision.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    twm...

    I'm being a bonehead...I got this part working...thanks for your input. My problem was not using the correct type for 'b1'. I fixed that and it's ok. I have a new problem where I first use the function 'fgets' to read how many words there are in the file. Then after I do that I use fgets again to read each data element again. The problem is, is that after the first use of 'fgets' I'm at the end of the file. So when I use 'fgets' the second time on the same file it doesn't read anything because I 'm at the end of the file. Is there a way to rewind or point back to the beginning of the file? Or should I just store off the data when I read the file using 'fgets' the first time?
    Thanks!
    Flight

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Thumbs up twm, never mind...figured it out..

    thanks a bunch for your input...I should have been more patient with myself and just figured it out (like I did) before I posted. I'm still glad I asked the questions though. You showed me alternative methods that I will use in the future. Tanks again!
    Flight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficiency comparing ints and floats
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 03:56 PM
  2. ints & floats
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 02-28-2009, 03:41 PM
  3. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  4. determining floats or ints
    By quiet_forever in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2007, 07:11 PM
  5. Replies: 2
    Last Post: 08-05-2002, 02:19 PM