Thread: file reading problem

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    39

    file reading problem

    Hi everyone - can someone pls help me with this issue i am having with reading a file -

    I am trying to read a file of 5 columns of data - and then i have to analyse the 3rd, 4th and 5th column.



    The data looks like this - (its all clustered together below - but the first row is = 1....1....14.027...3.474....1.964 etc etc)

    1 1 14.027 3.474 1.964
    1 2 13.936 3.445 1.967
    1 3 14.055 3.457 1.875
    1 4 14.019 3.468 1.953
    2 5 15.084 2.893 4.812
    2 6 15.158 2.912 4.870
    2 7 15.107 2.935 4.729





    This is the program I am using to read the file and save the 3rd. 4th and 5th in arrays called x, y and z

    Code:
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <signal.h>
    
    
    main (int argc, char **argv)
    {
     int i, j, p, q, count, d[20];
     double a, b, c, r[20];
     double x[3492][4], y[3492][4], z[3492][4];
     FILE *fp;
     fp = fopen ("1.gro", "r");
     for (i=0; i<3492; i++)
     for (j=0; j<4; j++)
         {
         if (j==0 || j==3)
            fscanf(fp, "%5.6d%5.2d%lf___%lf___%lf", p, q, &x[i][j], &y[i][j], &z[i][j]);
         if (j==1 || j==2)
            fscanf(fp, "%5.5d%5.2d%lf___%lf___%lf", p, q, &x[i][j], &y[i][j], &z[i][j]);  
         }
    
     .
     .
     .
    fclose(fp);
    return 0;
    }



    However when i print out what i read in as x[i] [j] and y[i] [j] and z[i] [j] - whether on screen or in a new file, i get lots of -


    325534197301 -1.6f_0.000000_0.000000
    3255341973014197510.6f_0.000000_0.000000
    3255341973014197510.6f_0.000000_0.000000
    3255341973014197484.6f_0.000000_0.000000







    Can someone pls explain what I was doing wrong - thanks a lot.
    Last edited by but; 03-15-2016 at 12:03 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The scanf functions don't accept a "precision" sub-specifier, i.e., the ".6" part of "%5.6d" has no meaning in the scanf functions. Your compiler should have warned you about that. Use a higher warning level if it didn't.

    And why are there all those underscores in your format? They would have to be in the input for a match to occur, but your input doesn't have them.

    Anyway, all you need is:
    Code:
        fscanf(fp, "%*d%*d%lf%lf%lf", &x, &y, &z);
    The asterisks tell fscanf to read and discard the field, so we don't need p and q to store them in.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Why are x, y and z 2D arrays? They should only be 1D arrays:
    Code:
    #define MAX_ROWS 3492
    
    /* ... */
    
    double x[MAX_ROWS];
    double y[MAX_ROWS];
    double z[MAX_ROWS];
    Furthermore, unless it is obvious from context, e.g., because they denote the axes of 3D space or something like that, those are poor names for such arrays. You should use descriptive names. Glossing over this though, your code can then be:
    Code:
    for (i = 0; i < MAX_ROWS && fscanf(fp, "%*d%*d%lf%lf%lf", &x[i], &y[i], &z[i]) == 3; i++)
    {
        /* do nothing here... or validate? */
    }
    Notice that with MAX_ROWS instead of a magic number, changing the maximum number of rows becomes simpler.

    You should also check the return value of fopen before proceeding to read from (or write to, in other cases) the file.
    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

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    ok so if i make them 1D arrays, then how to I save the data - i want one array of every 1st, 5th, 9th etc, another for 2nd, 6th and 10th etc.

    That was the motivation of taking 2D array- obviously wrong - so whats the right way to do it - thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Quote Originally Posted by but
    ok so if i make them 1D arrays, then how to I save the data - i want one array of every 1st, 5th, 9th etc, another for 2nd, 6th and 10th etc.
    Save the data into one array each, then you can always access x[0], x[4], x[8], then x[1], x[5], x[9], etc.
    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

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    hi all again - thanks to your inputs, i got the program to work - now i have a different problems

    i have to replicate the program across many sets of data, saved i the same file -

    suppose the relevant data is 100 lines. The file has over 20,000 lines.

    The way the file is written is -

    the first 2 lines are some useless info, in the wrong format, that i need to skip reading or delete altogether.
    Then come the 100 lines of relevant data
    than another useless line (i.e the 103-th line is useless)

    then this patterns repeats again about 200 more times

    So i tried to make the program NOT read the first 2 and the 103th line - but again got the core dump problem.

    So how to negotiate this - one is to save it in a 2d array (one dimension for the number of sets, and another for the 100 lines in each set), while skipping the first 2 and the last (103th) line - but it is not working.

    Code:
    for (j=0; j<200; j++)
         {
         for (i=0; i<103; i=i+1)
            {
    if (i!=103 || i!=0 || 1!=1)         //   this line is not doing its job   //
    {
    fscanf(fp, "%*d%*d%lf%lf%lf", &x[i][j], &y[i][j], &z[i][j]);
    }
    }
    }
    The other is to delete the first 2 and the last 103th line of each set and then read the "massaged" data.
    Last edited by but; 03-25-2016 at 03:08 PM.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Do you mean something like this:
    Code:
    #define NUMBER_OF_GROUPS  200
    #define LINES_PER_GROUP   100
    
    // ...
    
        fgets(line, sizeof line, fp);  // skip first two lines
        fgets(line, sizeof line, fp);
    
        for (int i = 0; i < NUMBER_OF_GROUPS; i++) {
            for (int j = 0; j < LINES_PER_GROUP; j++)
                fscanf(fp, "%*d%*d%lf%lf%lf", &x[i][j], &y[i][j], &z[i][j]);
            fgets(line, sizeof line, fp);  // skip single line
        }

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    no there is one block of data in a file -

    the first 2 lines are bogus and in the wrong format, then 100 lines that i need, then one more bogus line.
    Then this repeats again, for 20 more times.

    So how to make the program skip/delete lines 1, 2, then read 3-102, then skip/delete 103, then skip/delete 104 and 105, then read 106-205, then skip/delete 206 and so on

    my code attempted to skip - but was not successful.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Do you understand my example?
    If so, can you modify it to do what you want?
    It sounds like it should be easy.

  10. #10
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    is fget used to skip lines?

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by but View Post
    is fget used to skip lines?
    Yes, I'm using fgets to read an entire line that we then ignore.
    You would also need to declare a line string, something like:
    Code:
    char line[1024];

  12. #12
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    so should my code look like this -

    Code:
    fgets(line, sizeof line, fp);  // skip first two lines
    Code:
        fgets(line, sizeof line, fp);
    
        for (int i = 0; i < NUMBER_OF_GROUPS; i++) {
            for (int j = 0; j < LINES_PER_GROUP; j++)
                fscanf(fp, "%*d%*d%lf%lf%lf", &x[i][j], &y[i][j], &z[i][j]);
            fgets(line, sizeof line, fp);  // skip single line
        }
    

  13. #13
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    thats not what i wanted to post - but now i cant edit - here is what i wanted to say - is this what i must code ??

    Code:
        for (int i = 0; i < NUMBER_OF_GROUPS; i++) {
    
    
    fgets(line, sizeofline, fp);  // skip first two lines
    fgets(line, sizeof line, fp);
    
    for (int j = 0; j < LINES_PER_GROUP; j++)
                fscanf(fp, "%*d%*d%lf%lf%lf", &x[i][j], &y[i][j], &z[i][j]);
            fgets(line, sizeof line, fp);  // skip single line
        }
    

  14. #14
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    Quote Originally Posted by algorism View Post
    You would also need to declare a line string, something like:
    Code:
    char line[1024];
    Pls explain this line - the lines i want to read (not the 3 i want to discard) have a certain pattern - pls see my earlier posts in this thread - and are NOT characters.

    Thanks

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Quote Originally Posted by but
    Pls explain this line - the lines i want to read (not the 3 i want to discard) have a certain pattern - pls see my earlier posts in this thread - and are NOT characters.
    You need that to read and discard the lines that you want to discard.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading from file... problem with strings
    By Ruben Marques in forum C Programming
    Replies: 58
    Last Post: 07-02-2014, 03:23 PM
  2. Problem with reading of file
    By skumarisation in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2010, 06:01 AM
  3. file reading problem
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2005, 08:58 AM
  4. Problem reading a file
    By caduardo21 in forum C Programming
    Replies: 8
    Last Post: 04-18-2005, 11:51 AM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

Tags for this Thread