problem with reading floatting numbers

This is a discussion on problem with reading floatting numbers within the C Programming forums, part of the General Programming Boards category; Hi i want to read float number from file .txt without using vectors , umbers are seprated by space , ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    57

    problem with reading floatting numbers

    Hi
    i want to read float number from file .txt without using vectors , umbers are seprated by space , the nfor that i make like that
    Code:
    if (fp != NULL)
    
         {
    
             while (fgets(line, 1000, fp))
    
             {
                 while(line[counter+1]!=NULL)
                 {
    if (line[counter]==' ')
                     {
    number=number/(Ogre::Math::Pow(10,number_float));
                            printf ("le nombre= %f",number);
    }
    else
                     {
                         
                    if (line[counter]=='.') 
                    {
    
                        number_float=0;
                        fl=true;
                        
                    }
                    else
                    {
                    if(fl==false)
                    {
                        number=number*10+((int)line[counter]-(int)'0');
                        
                    }
                    else 
                    {
                        number_float++;
                        number=number*10+((int)line[counter]-(int)'0');
                     }
                    }
                     }
    counter++;
    that's seem work
    but now i have problem with signe '+' , '-' i do not know how i make to read the positif and negatif numbers
    have you an idea ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    strtod(3) - Linux man page
    Using endptr, you can step through a whole string of space-separated numbers, and extract each one in turn.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    hi i did not understand how it work
    in my file.txt the data are like that
    0.10 -12.154 +0.002

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Like so
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    int main ()
    {
        char line[] = "0.10 -12.154 +0.002 1e1000\n";  // like fgets gives you
        char *p = line, *endp;
        while ( *p != '\0' ) {
            double d = strtod(p,&endp);
            if ( p == endp ) break;
            if ( errno == 0 ) {
                printf("Found value=%f\n", d );
                p = endp;
            } else {
                perror("Failed");
                break;
            }
        }
        return 0;
    }
    $ ./a.out 
    Found value=0.100000
    Found value=-12.154000
    Found value=0.002000
    Failed: Numerical result out of range
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    thanks for the code , but what represent " errno" , you did not initialize it

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    "errno" is a macro (defined in <errno.h>) used by many standard functions to store an error code in case of errors.

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    BUT if i do not delete " if ( errno == 0 ) {" i will enter in infinite loop

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    It's necessary code.

    What is your problem with checking a function for errors, by examining the errno variable declared in errno.h?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    Hi sorry i just see the reply .

    id not know where is exactelly the problem ,
    when i add if ( errno == 0) it do not execute " printf("Found value=%f\n", d );
    p = endp;"
    but it pront to me : failed:no such file or directory
    i see the value of errno and i found it : 0.00000 so why it do not enter ?

    i have another question , i have this struct
    Code:
    struct  joint_angle
        {
             int   count;
    
            Ogre::Vector3  orient;
        }J;
         struct pose
         {
            float time;
           std::vector<joint_angle> angle;
         }P;
    where time is the first number(float) in the line
    so , i must store each line in "pose"
    but it does not work for me

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Are you aware that this is C++ and not C?
    In addition let me inform you that we are in the C thread. The forum has a C++ thread.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    hi,
    yes i know that i am in c forum ,
    but because i hvae some lines in c, and the others are in c++ i am not sure where i put my question

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    No this is not my point. This is the best forum I know for C++ and C. Might be for C#, but I am not familiar with this language, so I can't judge.

    If you see in the main page of the forum, there are three threads, one for C++, one for C and one for C#.

    By posting in the right one, you have more chances of someone familiar with C++ to help you.

    Also I would suggest not to go and post to the C++ thread now. This is something to remember for the next time you post.
    If you do not find the post here, this means that it has been moved to the C++ thread.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    thanks for the suggestion

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    57
    i see that's no reply , for that i will transfer it to the c++ forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading numbers from console
    By gerrard in forum C Programming
    Replies: 3
    Last Post: 10-23-2009, 04:14 AM
  2. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  3. Reading numbers from txt file
    By Ene Dene in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2006, 05:12 PM
  4. Reading Numbers from files...
    By Junior89 in forum C++ Programming
    Replies: 36
    Last Post: 07-31-2005, 07:45 AM
  5. Reading numbers from a file
    By Zoalord in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21