Thread: how to read specfic columns of data in a file.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    15

    how to read specfic columns of data in a file.

    Hi I wish to write code that allows me to access the specific column of 1, 2 and 4 respectively. That is detector number, crystal number and theta columns in the data configuration file shown below.
    The configuration file I wish to access has data written in this format.

    CONFIGURATION FILE
    1st column: detector number
    2nd column: crystal number
    3rd column: ring
    4th column: theta
    5th column: phi
    6th column: include flag (0 to throw away)

    1 1 1 157.6 0 1
    2 2 1 157.6 72 1
    3 3 1 157.6 144 1
    4 4 1 157.6 216 1
    5 5 1 157.6 288 1
    6 6 2 133.5 18 1
    7 7 2 133.5 54 1
    8 8 2 133.5 90 1
    9 9 2 133.5 126 1
    10 10 2 133.5 162 1
    11 11 2 133.5 198 1
    12 12 2 133.5 234 1
    13 13 2 133.5 270 1
    14 14 2 133.5 306 1
    15 15 2 133.5 342 1
    16 16 1 109.0 19.5 1
    16 17 1 109.0 10.5 1
    16 18 2 100.0 10.5 1
    16 19 2 100.0 19.5 1
    17 20 1 109.0 49.5 1
    17 21 1 109.0 40.5 1
    17 22 2 100.0 40.5 1
    17 23 2 100.0 49.5 1
    18 24 1 109.0 79.5 1
    18 25 1 109.0 70.5 1
    18 26 2 100.0 70.5 1
    18 27 2 100.0 79.5 1
    19 28 1 109.0 109.5 1
    19 29 1 109.0 100.5 1
    19 30 2 100.0 100.5 1
    19 31 2 100.0 109.5 1
    20 32 1 109.0 139.5 1
    20 33 1 109.0 130.5 1
    20 34 2 100.0 130.5 1
    20 35 2 100.0 139.5 1
    21 36 1 109.0 169.5 1
    21 37 1 109.0 160.5 1
    21 38 2 100.0 160.5 1
    21 39 2 100.0 169.5 1
    22 40 1 109.0 199.5 1
    22 41 1 109.0 190.5 1
    22 42 2 100.0 190.5 1
    22 43 2 100.0 199.5 1
    23 44 1 109.0 229.5 1
    23 45 1 109.0 220.5 1
    23 46 2 100.0 220.5 1
    23 47 2 100.0 229.5 1
    24 48 0 0 0 0
    24 49 0 0 0 0
    24 50 0 0 0 0
    24 51 0 0 0 0
    25 52 1 109.0 289.5 1
    25 53 1 109.0 280.5 1
    25 54 2 100.0 280.5 1
    25 55 2 100.0 289.5 1
    26 56 1 109.0 319.5 1
    26 57 1 109.0 310.5 1
    26 58 2 100.0 310.5 1
    26 59 2 100.0 319.5 1
    27 60 1 109.0 349.5 1
    27 61 1 109.0 340.5 1
    27 62 2 100.0 340.5 1
    27 63 2 100.0 349.5 1
    28 64 4 71.0 10.5 1
    28 65 4 71.0 19.5 1
    28 66 3 80.0 19.5 1
    28 67 3 80.0 10.5 1
    29 68 4 71.0 40.5 1
    29 69 4 71.0 49.5 1
    29 70 3 80.0 49.5 1
    29 71 3 80.0 40.5 1
    30 72 4 71.0 70.5 1
    30 73 4 71.0 79.5 1
    30 74 3 80.0 79.5 1
    30 75 3 80.0 70.5 1
    31 76 4 71.0 100.5 1
    31 77 4 71.0 109.5 1
    31 78 3 80.0 109.5 1
    31 79 3 80.0 100.5 1
    32 80 4 71.0 130.5 1
    32 81 4 71.0 139.5 1
    32 82 3 80.0 139.5 1
    32 83 3 80.0 130.5 1
    33 84 4 71.0 160.5 1
    33 85 4 71.0 169.5 1
    33 86 3 80.0 169.5 1
    33 87 3 80.0 160.5 1
    34 88 4 71.0 190.5 1
    34 89 4 71.0 199.5 1
    34 90 3 80.0 199.5 1
    34 91 3 80.0 190.5 1
    35 92 4 71.0 220.5 1
    35 93 4 71.0 229.5 1
    35 94 3 80.0 229.5 1
    35 95 3 80.0 220.5 1
    36 96 4 71.0 250.5 1
    36 97 4 71.0 259.5 1
    36 98 3 80.0 259.5 1
    36 99 3 80.0 250.5 1
    37 100 4 71.0 280.5 1
    37 101 4 71.0 289.5 1
    37 102 3 80.0 289.5 1
    37 103 3 80.0 280.5 1
    38 104 4 71.0 310.5 1
    38 105 4 71.0 319.5 1
    38 106 3 80.0 319.5 1
    38 107 3 80.0 310.5 1
    39 108 4 71.0 340.5 1
    39 109 4 71.0 349.5 1
    39 110 3 80.0 349.5 1
    39 111 3 80.0 340.5 1

    So can anyone help me with how I can write code in my program to access this configuration file and read the data only in columns 1,2 and 4
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Open file. fopen()
    Read line using fgets().
    Parse the line using sscanf().
    repeat until end of file.
    Close file. fclose()

    Post properly indented code in code tag if you have problem.
    Last edited by Bayint Naung; 06-12-2010 at 03:15 AM. Reason: ps

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine you had 4 int's in a row of data, and you only wanted the first and third int's:
    Code:
    int n1, n3, garbage;
    
    fscanf(myFilePointer, "%d %d %d %d", &n1, &garbage, &n3, &garbage);
    Using fgets and then using sscanf(), with this kind of idea, is a great way to go. sscanf() works a lot like fscanf(), of course.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can just use assignment-suppression character *.
    Code:
       
          fscanf(fin,"%d %*d %d %*d",&n1,&n3);

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Ok thanks everyone, I can read the data from the file. now I need to write code that connects the crystal numbers above 15 to the detector number since there are 4 crystals in each detector above detctor number 15..
    For example

    if there is an event (something is detected) in crystal 110 and 111 and I want to add these two events together how can I do that . I need to somehow assign crystal number 110 and 111 to detector 39.

    So that I can get any event which occurs in the same detcector and add them together.

    I started writing the code but became stuck. event fold by the way is just the number of events detected in a specified period of time.


    for(i=1;i<=event_fold;i++)

    if(event_fold >1 && detector[crystal_number[i]] > 15 )



    Any help would be appreciated.
    Last edited by zidangus; 06-16-2010 at 07:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  5. read data out of *.txt file
    By dune911 in forum C Programming
    Replies: 3
    Last Post: 12-14-2001, 12:33 PM