Thread: how to get data from a file in c

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    how to get data from a file in c

    Hi,

    I'd like to get only the third column of this file into a c-programm and to calculate the mean value of that numbers. Can somebody help me?

    Thanks


    02/01/2007 01.10.00 -2,0 G
    02/01/2007 01.20.00 -2,0 G
    02/01/2007 01.30.00 -2,1 G
    02/01/2007 01.40.00 -2,0 G
    02/01/2007 01.50.00 -2,3 G
    02/01/2007 02.00.00 -2,4 G
    02/01/2007 02.10.00 -2,1 G
    02/01/2007 02.20.00 -1,9 G
    02/01/2007 02.30.00 -2,1 G
    02/01/2007 02.40.00 -2,2 G
    02/01/2007 02.50.00 -2,1 G
    02/01/2007 03.00.00 -2,1 G

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use fgets() to read one line at a time and then, for each line, use sscanf() [Note the two s's] to parse the line and obtain/extract the values needed. Based on those values, calculate the mean.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    char* third_column(char* str)
    {
        char* ret = NULL;
        int space = 0;
        while( *str ) {
            space += *str == ' ';
            switch( space ) {
                case 2:
                    if( *str == ',' ) { *str = '.'; }
                    if( !ret ) { ret = str; }
                break;
                case 3:
                    *str = '\0';
                    return ret;
                break;
            }
            ++str;
        }
        return ret;
    }
    Last edited by zub; 08-16-2014 at 11:09 AM.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    zub: I understand that you may find it easier to post code than to suggest a solution in English (or pseudocode), but it is poor form to post complete code for a significant function that could well be used as-is before the help seeker has shown an attempt. That said, I think your interpretation of the third column is (deliberately?) wrong, but it is still spoonfeeding. Please do not do this again.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by laserlight View Post
    That said, I think your interpretation of the third column is (deliberately?) wrong, but it is still spoonfeeding.
    Code:
    #include "greatest.h"
    
    char* third_column(char* str)
    {
        char* ret = NULL;
        int space = 0;
        while( *str ) {
            space += *str == ' ';
            switch( space ) {
                case 2:
                    if( *str == ',' ) { *str = '.'; }
                    if( !ret ) { ret = str; }
                break;
                case 3:
                    *str = '\0';
                    return ret;
                break;
            }
            ++str;
        }
        return ret;
    }
    
    
    TEST third_column_1()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.10.00 -2,0 G")), -2.0);
        PASS();
    }
    
    
    TEST third_column_2()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.20.00 -2,0 G")), -2.0);
        PASS();
    }
    
    
    TEST third_column_3()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.30.00 -2,1 G")), -2.1);
        PASS();
    }
    
    
    TEST third_column_4()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.40.00 -2,0 G")), -2.0);
        PASS();
    }
    
    
    TEST third_column_5()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.50.00 -2,3 G")), -2.3);
        PASS();
    }
    
    
    TEST third_column_6()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.00.00 -2,4 G")), -2.4);
        PASS();
    }
    
    
    TEST third_column_7()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.10.00 -2,1 G")), -2.1);
        PASS();
    }
    
    
    TEST third_column_8()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.20.00 -1,9 G")), -1.9);
        PASS();
    }
    
    
    TEST third_column_9()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.30.00 -2,1 G")), -2.1);
        PASS();
    }
    
    
    TEST third_column_10()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.40.00 -2,2 G")), -2.2);
        PASS();
    }
    
    
    TEST third_column_11()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.50.00 -2,1 G")), -2.1);
        PASS();
    }
    
    
    TEST third_column_12()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 03.00.00 -2,1 G")), -2.1);
        PASS();
    }
    
    
    SUITE(test_third_column)
    {
        RUN_TEST(third_column_1);
        RUN_TEST(third_column_2);
        RUN_TEST(third_column_3);
        RUN_TEST(third_column_4);
        RUN_TEST(third_column_5);
        RUN_TEST(third_column_6);
        RUN_TEST(third_column_7);
        RUN_TEST(third_column_8);
        RUN_TEST(third_column_9);
        RUN_TEST(third_column_10);
        RUN_TEST(third_column_11);
        RUN_TEST(third_column_12);
    }
    
    
    GREATEST_MAIN_DEFS();
    
    
    int main(int argc, char **argv) {
        GREATEST_MAIN_BEGIN();      /* command-line arguments, initialization. */
        RUN_SUITE(test_third_column);
        GREATEST_MAIN_END();        /* display results */
    }
    C:\UTIL\tcc>tcc.exe -run -Wall run.c


    * Suite test_third_column:
    ............
    12 tests - 12 pass, 0 fail, 0 skipped (4 ticks, 0.004 sec)


    Total: 12 tests (9 ticks, 0.009 sec)
    Pass: 12, fail: 0, skip: 0.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When he said your interpretation is wrong, he didn't mean that the code is broken. In particular, I think he means you are assuming it is ok to change a datum like "2,0" into "2.0". Whether that interpretation is right or wrong, it is also true that you are doing people's homework for them which is called spoonfeeding.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    When he said your interpretation is wrong, he didn't mean that the code is broken. In particular, I think he means you are assuming it is ok to change a datum like "2,0" into "2.0".
    Indeed. zub, compile and run:
    Code:
    #include "greatest.h"
     
    char* third_column(char* str)
    {
        char* ret = NULL;
        int space = 0;
        while( *str ) {
            space += *str == ' ';
            switch( space ) {
                case 2:
                    if( *str == ',' ) { *str = '.'; }
                    if( !ret ) { ret = str; }
                break;
                case 3:
                    *str = '\0';
                    return ret;
                break;
            }
            ++str;
        }
        return ret;
    }
    
    TEST third_column_1()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.10.00 -2,0 G")), -1.0);
        PASS();
    }
     
     
    TEST third_column_2()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.20.00 -2,0 G")), -1.0);
        PASS();
    }
     
     
    TEST third_column_3()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.30.00 -2,1 G")), -0.5);
        PASS();
    }
     
     
    TEST third_column_4()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.40.00 -2,0 G")), -1.0);
        PASS();
    }
     
     
    TEST third_column_5()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 01.50.00 -2,3 G")), -0.5);
        PASS();
    }
     
     
    TEST third_column_6()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.00.00 -2,4 G")), 1.0);
        PASS();
    }
     
     
    TEST third_column_7()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.10.00 -2,1 G")), -0.5);
        PASS();
    }
     
     
    TEST third_column_8()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.20.00 -1,9 G")), 4.0);
        PASS();
    }
     
     
    TEST third_column_9()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.30.00 -2,1 G")), -0.5);
        PASS();
    }
     
     
    TEST third_column_10()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.40.00 -2,2 G")), 0.0);
        PASS();
    }
     
     
    TEST third_column_11()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 02.50.00 -2,1 G")), -0.5);
        PASS();
    }
     
     
    TEST third_column_12()
    {
        ASSERT_EQ(atof(third_column("02/01/2007 03.00.00 -2,1 G")), -0.5);
        PASS();
    }
    
    SUITE(test_third_column)
    {
        RUN_TEST(third_column_1);
        RUN_TEST(third_column_2);
        RUN_TEST(third_column_3);
        RUN_TEST(third_column_4);
        RUN_TEST(third_column_5);
        RUN_TEST(third_column_6);
        RUN_TEST(third_column_7);
        RUN_TEST(third_column_8);
        RUN_TEST(third_column_9);
        RUN_TEST(third_column_10);
        RUN_TEST(third_column_11);
        RUN_TEST(third_column_12);
    }
     
     
    GREATEST_MAIN_DEFS();
     
     
    int main(int argc, char **argv) {
        GREATEST_MAIN_BEGIN();      /* command-line arguments, initialization. */
        RUN_SUITE(test_third_column);
        GREATEST_MAIN_END();        /* display results */
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Yes, I misunderstood the task. I thought topicstarter come from a country where the decimal point is written with a comma (as in Russia).
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  2. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  3. Replies: 2
    Last Post: 05-06-2011, 07:43 AM
  4. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  5. Replies: 21
    Last Post: 11-03-2007, 02:56 PM